简体   繁体   English

使iOS插件统一打开相机和照片库

[英]make iOS plugin to open camera and photo gallery from unity

I have game in unity and I want to let the user change the background from camera or photo gallery in IOS, I know that I have to make library in objective c , and I know how to open the camera or photo gallery in objective c using this code 我有一个统一的游戏,我想让用户更改IOS中相机或照片库的背景,我知道我必须在目标c中创建库,并且我知道如何使用以下方法在目标c中打开相机或照片库此代码

-(void)getImage{

    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
    imagePickerController.delegate = self;
    imagePickerController.sourceType =  UIImagePickerControllerCameraCaptureModePhoto;
    [self presentViewController:imagePickerController animated:YES completion:nil];

}


- (void) imagePickerControllerDidCancel:(UIImagePickerController *) picker {

    [picker dismissViewControllerAnimated:YES completion:^{}];
}

- (void)imagePickerController : (UIImagePickerController *) picker didFinishPickingImage:(UIImage *)imageProfile editingInfo:(NSDictionary *) editingInfo
{

    [picker dismissModalViewControllerAnimated:YES];

}

but how to use this code in objective c library 但是如何在目标C库中使用此代码

If you want to use the Camera Picker for Unity for a image gallery i suggest you to write a plugin wrapping the API you are describing yourself. 如果您想将Camera Picker for Unity用于图片库,建议您编写一个包装自己描述的API的插件。 Something like : 就像是 :

extern "C" char* _ImagePickerOpen ( int type, bool frontFacingIsDefault, char* callbackName ) {

Then inside ImagePickerOpen you should be able to initialize the Gallery with the code you already provided yourself. 然后在ImagePickerOpen内部,您应该能够使用自己提供的代码初始化Gallery。

When an image has been picked i suggest you to use something like this code to save the image to the disk and return the url for unity to use it. 选择图像后,建议您使用类似以下代码的代码将图像保存到磁盘,并返回URL以统一使用。 This is one approach but you can also base64 encode it and return it as one big string. 这是一种方法,但是您也可以对它进行base64编码,然后将其作为一个大字符串返回。

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(NSDictionary*)info {
callCount++;

// Image :
image = [self scaleAndRotateImage:image];

// Image data:
NSData *imageData = UIImageJPEGRepresentation(image,0.6);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"temp.jpg"];

NSString *fileUri=[NSString stringWithFormat:@"file:%@?ori=%d&cnt=%d",path,image.imageOrientation,callCount];

strcpy( fileUriStr, (char*)[fileUri UTF8String] );

[imageData writeToFile:path atomically:YES];

self.lastImageUri = fileUri;
[self.imagePicker.presentingViewController dismissModalViewControllerAnimated:YES];

UnityGaleryCallback( fileUri );}

The code above is from a plugin which we made at one time. 上面的代码来自我们一次制作的插件。 If you are looking to use the ImagePicker to take photos with Apple's buildin photo functionality and not only gallery i suggest you to look into something like Camera Capture Kit - ( https://www.assetstore.unity3d.com/en/#!/content/56673 )as well since that should allow you abit more control on the image captured. 如果您要使用ImagePicker来拍摄带有Apple内置照片功能的照片,而不仅仅是图库,我建议您考虑使用Camera Capture Kit-( https://www.assetstore.unity3d.com/zh/#! content / 56673 ),因为那样可以让您对所捕获的图像进行更多控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM