制作iOS插件,从团结中打开相机和照片库

时间:2016-01-14 17:30:56

标签: c# ios objective-c unity3d

我有团结游戏,我想让用户在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];

}

但如何在目标c库中使用此代码

1 个答案:

答案 0 :(得分:0)

如果你想将Unity Picker用于图片库我建议你写一个包装你自己描述的API的插件。类似的东西:

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

然后在ImagePickerOpen中,您应该能够使用您自己提供的代码初始化Gallery。

当选择图像时,我建议您使用类似此代码的内容将图像保存到磁盘并返回统一网址以使用它。这是一种方法,但您也可以对其进行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 );}

上面的代码来自我们一次制作的插件。如果您希望使用ImagePicker拍摄具有Apple内置照片功能的照片而不仅仅是画廊,我建议您查看类似于Camera Capture Kit的内容 - (https://www.assetstore.unity3d.com/en/#!/content/56673),因为这样可以让您更好地控制捕获的图像。