unityios相册_【Ios】 Unity for iOS 打开相册、相机及保存图片到相册

这个教程展示了如何在Unity iOS应用中打开相册、调用相机以及将图片保存到相册。通过`UIAlertController`展示选项让用户选择相册、相机或两者,并使用`UIImagePickerController`进行操作。当用户选择图片后,会进行图像处理并将其以Base64编码形式回传给Unity。同时,提供了保存图片到相册的函数。
摘要由CSDN通过智能技术生成

//

//  OpenPhotoController.m

//

//  Created by AnYuanLzh

//

#import "IOSAlbumCameraController.h"

@implementationIOSAlbumCameraController

- (void)showActionSheet

{

NSLog(@" --- showActionSheet !!");

UIAlertController*alertController = [UIAlertControlleralertControllerWithTitle:nilmessage:nilpreferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction*albumAction = [UIAlertActionactionWithTitle:@"相册"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction* _Nonnull action) {

NSLog(@"click album action!");

[selfshowPicker:UIImagePickerControllerSourceTypePhotoLibraryallowsEditing:YES];

}];

UIAlertAction*cameraAction = [UIAlertActionactionWithTitle:@"相机"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction* _Nonnull action) {

NSLog(@"click camera action!");

[selfshowPicker:UIImagePickerControllerSourceTypeCameraallowsEditing:YES];

}];

UIAlertAction*album_cameraAction = [UIAlertActionactionWithTitle:@"相册&相机"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction* _Nonnull action) {

NSLog(@"click album&camera action!");

//[self showPicker:UIImagePickerControllerSourceTypeCamera];

[selfshowPicker:UIImagePickerControllerSourceTypeSavedPhotosAlbumallowsEditing:YES];

}];

UIAlertAction*cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction* _Nonnull action) {

NSLog(@"click cancel action!");

}];

[alertControlleraddAction:albumAction];

[alertControlleraddAction:cameraAction];

[alertControlleraddAction:album_cameraAction];

[alertControlleraddAction:cancelAction];

UIViewController*vc = UnityGetGLViewController();

[vcpresentViewController:alertControlleranimated:YEScompletion:^{

NSLog(@"showActionSheet -- completion");

}];

}

- (void)showPicker:

(UIImagePickerControllerSourceType)type

allowsEditing:(BOOL)flag

{

NSLog(@" --- showPicker!!");

UIImagePickerController*picker = [[UIImagePickerControlleralloc]init];

picker.delegate=self;

picker.sourceType= type;

picker.allowsEditing= flag;

[selfpresentViewController:pickeranimated:YEScompletion:nil];

}

// 打开相册后选择照片时的响应方法

- (void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary*)info

{

NSLog(@" --- imagePickerController didFinishPickingMediaWithInfo!!");

// Grab the image and write it to disk

UIImage*image;

UIImage*image2;

image = [infoobjectForKey:UIImagePickerControllerEditedImage];

UIGraphicsBeginImageContext(CGSizeMake(256,256));

[imagedrawInRect:CGRectMake(0,0,256,256)];

image2= UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

// 得到了image,然后用你的函数传回u3d

NSData*imgData;

if(UIImagePNGRepresentation(image2) ==nil)

{

NSLog(@" --- actionSheet slse!! 11 ");

imgData= UIImageJPEGRepresentation(image, .6);

}

else

{

NSLog(@" --- actionSheet slse!! 22 ");

imgData= UIImagePNGRepresentation(image2);

}

NSString*_encodeImageStr = [imgDatabase64Encoding];

UnitySendMessage( "UIUpload2(Clone)","PickImageCallBack_Base64", _encodeImageStr.UTF8String);

// 关闭相册

[pickerdismissViewControllerAnimated:YEScompletion:nil];

}

// 打开相册后点击“取消”的响应

- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker

{

NSLog(@" --- imagePickerControllerDidCancel !!");

[selfdismissViewControllerAnimated:YEScompletion:nil];

}

//- (void)dismissWrappedController

//{

//    NSLog(@" --- dismissWrappedController !!");

//    UIViewController *vc = UnityGetGLViewController();

//

//    if( !vc )

//      return;

//

//    [self performSelector:@selector(removeAndReleaseViewControllerWrapper) withObject:nil afterDelay:1.0];

//

//}

//- (void)removeAndReleaseViewControllerWrapper

//{

//    NSLog(@" --- removeAndReleaseViewControllerWrapper !!");

//  // iPad might have a popover

//  if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && _popoverViewController )

//  {

//      [_popoverViewController dismissPopoverAnimated:YES];

//      self.popoverViewController = nil;

//  }

//}

// (void) saveImageToPhotosAlbum:(NSString*) base64

//{

//    NSLog(@"base64: ");

//    NSLog(base64);

//    const char* string = [base64 UTF8String];

//    NSData* imageData = [NSData dataWithBytes:string length:strlen(string) 1];

//    UIImage* image = [UIImage imageWithData:imageData];

//    UIImageWriteToSavedPhotosAlbum(image,

//                                   self,

//                                   @selector(image:didFinishSavingWithError:contextInfo:),

//                                   NULL);

//}

(void)saveImageToPhotosAlbum:(NSString*) readAdr

{

NSLog(@"readAdr: ");

NSLog(readAdr);

UIImage* image = [UIImageimageWithContentsOfFile:readAdr];

UIImageWriteToSavedPhotosAlbum(image,

self,

@selector(image:didFinishSavingWithError:contextInfo:),

NULL);

}

(void)image:(UIImage*)imagedidFinishSavingWithError:(NSError*)errorcontextInfo:(void*)contextInfo

{

NSString* result;

if(error)

{

result = @"图片保存到相册失败!";

}

else

{

result = @"图片保存到相册成功!";

}

UnitySendMessage( "UIUpload2(Clone)","SaveImageToPhotosAlbumCallBack", result.UTF8String);

}

@end

//------------- called by unity -----begin-----------------

#if defined (__cplusplus)

extern"C"{

#endif

// 弹出一个菜单项:相册、相机

void_showActionSheet()

{

NSLog(@" -unity call-- _showActionSheet !!");

IOSAlbumCameraController* app = [[IOSAlbumCameraControlleralloc]init];

UIViewController*vc = UnityGetGLViewController();

[vc.viewaddSubview: app.view];

[appshowActionSheet];

}

// 打开相册

void_iosOpenPhotoLibrary()

{

if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])

{

IOSAlbumCameraController* app = [[IOSAlbumCameraControlleralloc]init];

UIViewController*vc = UnityGetGLViewController();

[vc.viewaddSubview: app.view];

[appshowPicker:UIImagePickerControllerSourceTypePhotoLibraryallowsEditing:NO];

}

else

{

UnitySendMessage( "UIUpload2(Clone)","PickImageCallBack_Base64", (@"").UTF8String);

}

}

// 打开相册

void_iosOpenPhotoAlbums()

{

if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])

{

IOSAlbumCameraController* app = [[IOSAlbumCameraControlleralloc]init];

UIViewController*vc = UnityGetGLViewController();

[vc.viewaddSubview: app.view];

[appshowPicker:UIImagePickerControllerSourceTypeSavedPhotosAlbumallowsEditing:NO];

}

else

{

_iosOpenPhotoLibrary();

}

}

// 打开相机

void_iosOpenCamera()

{

if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

{

IOSAlbumCameraController* app = [[IOSAlbumCameraControlleralloc]init];

UIViewController*vc = UnityGetGLViewController();

[vc.viewaddSubview: app.view];

[appshowPicker:UIImagePickerControllerSourceTypeCameraallowsEditing:NO];

}

else

{

UnitySendMessage( "UIUpload2(Clone)","PickImageCallBack_Base64", (@"").UTF8String);

}

}

// 打开相册--可编辑

void_iosOpenPhotoLibrary_allowsEditing()

{

if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])

{

IOSAlbumCameraController* app = [[IOSAlbumCameraControlleralloc]init];

UIViewController*vc = UnityGetGLViewController();

[vc.viewaddSubview: app.view];

[appshowPicker:UIImagePickerControllerSourceTypePhotoLibraryallowsEditing:YES];

}

else

{

UnitySendMessage( "UIUpload2(Clone)","PickImageCallBack_Base64", (@"").UTF8String);

}

}

// 打开相册--可编辑

void_iosOpenPhotoAlbums_allowsEditing()

{

if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])

{

IOSAlbumCameraController* app = [[IOSAlbumCameraControlleralloc]init];

UIViewController*vc = UnityGetGLViewController();

[vc.viewaddSubview: app.view];

[appshowPicker:UIImagePickerControllerSourceTypeSavedPhotosAlbumallowsEditing:YES];

}

else

{

_iosOpenPhotoLibrary();

}

}

// 打开相机--可编辑

void_iosOpenCamera_allowsEditing()

{

if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

{

IOSAlbumCameraController* app = [[IOSAlbumCameraControlleralloc]init];

UIViewController*vc = UnityGetGLViewController();

[vc.viewaddSubview: app.view];

[appshowPicker:UIImagePickerControllerSourceTypeCameraallowsEditing:YES];

}

else

{

UnitySendMessage( "UIUpload2(Clone)","PickImageCallBack_Base64", (@"").UTF8String);

}

}

// 保存照片到相册

//    void _iosSaveImageToPhotosAlbum(char* base64)

//    {

//        NSString* temp = [NSString stringWithUTF8String:base64];

//        [IOSAlbumCameraController saveImageToPhotosAlbum:temp];

//    }

void_iosSaveImageToPhotosAlbum(char* readAddr)

{

NSString* temp = [NSStringstringWithUTF8String:readAddr];

[IOSAlbumCameraControllersaveImageToPhotosAlbum:temp];

}

#if defined (__cplusplus)

}

#endif

//------------- called by unity -----end-----------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值