//
// 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-----------------