簡體   English   中英

使iOS插件統一打開相機和照片庫

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

我有一個統一的游戲,我想讓用戶更改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庫中使用此代碼

如果您想將Camera Picker for Unity用於圖片庫,建議您編寫一個包裝自己描述的API的插件。 就像是 :

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

然后在ImagePickerOpen內部,您應該能夠使用自己提供的代碼初始化Gallery。

選擇圖像后,建議您使用類似以下代碼的代碼將圖像保存到磁盤,並返回URL以統一使用。 這是一種方法,但是您也可以對它進行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/zh/#! content / 56673 ),因為那樣可以讓您對所捕獲的圖像進行更多控制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM