注:引擎版本unity4.x
最近项目需求,需要做用户头像,要求:
1. 可以从本地上传
2. 本地裁剪
3. 压缩控制大小
4. 在三个平台实现PC/Android/IOS
弄了好几天总是搞完了总结一下
在网上找了一段时间,有人推荐了一个插件,网盘地址忘记存了,直接上代码吧
OpenPhotoController.h :
@interface OpenPhotoController : NSObject<UIApplicationDelegate,UIImagePickerControllerDelegate, UIActionSheetDelegate,UINavigationControllerDelegate>
{
UIView* _rootView;
UIViewController* _rootController;
@private
id _popoverViewController;
}
@property (nonatomic, retain) id popoverViewController;
@end
OpenPhotoController.mm:
#import "OpenPhotoController.h" @implementation OpenPhotoController @synthesize popoverViewController = _popoverViewController; -(void)showActionSheet { UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:NSLocalizedString( @"取消", nil ) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString( @"拍照", nil ), NSLocalizedString( @"相册", nil ), nil]; if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) [sheet showFromRect:CGRectMake( 0, 0, 128, 128 ) inView:UnityGetGLViewController().view animated:YES]; else [sheet showInView:UnityGetGLViewController().view]; //[sheet release]; } - (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if( buttonIndex == 0 ) { [self showPicker:UIImagePickerControllerSourceTypeCamera]; } else if( buttonIndex == 1 ) { [self showPicker:UIImagePickerControllerSourceTypePhotoLibrary]; } else // Cancelled { //UnityPause( false ); //UnitySendMessage( "EtceteraManager", "imagePickerDidCancel", "" ); } } - (void)showPicker:(UIImagePickerControllerSourceType)type { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = type; picker.allowsEditing = YES; // We need to display this in a popover on iPad if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) { Class popoverClass = NSClassFromString( @"UIPopoverController" ); if( !popoverClass ) return; _popoverViewController = [[popoverClass alloc] initWithContentViewController:picker]; [_popoverViewController setDelegate:self]; //picker.modalInPopover = YES; // Display the popover [_popoverViewController presentPopoverFromRect:CGRectMake( 0, 0, 128, 128 ) inView:UnityGetGLViewController().view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; } else { // wrap and show the modal UIViewController *vc = UnityGetGLViewController(); [vc presentModalViewController:picker animated:YES]; } } - (void)popoverControllerDidDismissPopover:(UIPopoverController*)popoverController { self.popoverViewController = nil; //UnityPause( false ); //UnitySendMessage( "EtceteraManager", "imagePickerDidCancel", "" ); } - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info { // Grab the image and write it to disk UIImage *image; UIImage *image2; // if( _pickerAllowsEditing ) image = [info objectForKey:UIImagePickerControllerEditedImage]; // else // image = [info objectForKey:UIImagePickerControllerOriginalImage]; //NSLog( @"picker got image with orientation: %i", image.imageOrientation ); UIGraphicsBeginImageContext(CGSizeMake(128,128)); [image drawInRect:CGRectMake(0, 0, 128, 128)]; image2 = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // 得到了image,然后用你的函数传回u3d NSData *imgData; if(UIImagePNGRepresentation(image2) == nil) { imgData= UIImageJPEGRepresentation(image2, 0.5); } else { imgData= UIImageJPEGRepresentation(image2, 0.5); } NSString *_encodeImageStr = [imgData base64Encoding]; UnitySendMessage( "UnityIOSBridge", "PhotoCallBack", _encodeImageStr.UTF8String); // Dimiss the pickerController [self dismissWrappedController]; } - (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker { // dismiss the wrapper, unpause and notifiy Unity what happened [self dismissWrappedController]; //UnityPause( false ); //UnitySendMessage( "EtceteraManager", "imagePickerDidCancel", "" ); } - (void)dismissWrappedController { //UnityPause( false ); UIViewController *vc = UnityGetGLViewController(); // No view controller? Get out of here. if( !vc ) return; // dismiss the view controller [vc dismissModalViewControllerAnimated:YES]; // remove the wrapper view controller [self performSelector:@selector(removeAndReleaseViewControllerWrapper) withObject:nil afterDelay:1.0]; //UnitySendMessage( "EtceteraManager", "dismissingViewController", "" ); } - (void)removeAndReleaseViewControllerWrapper { // iPad might have a popover if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && _popoverViewController ) { [_popoverViewController dismissPopoverAnimated:YES]; self.popoverViewController = nil; } } @end extern "C" { void openPhoto()//相册 { //UnityPause( true ); OpenPhotoController * app = [[OpenPhotoController alloc] init]; // No need to give a choice for devices with no camera if( ![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ) { [app showPicker:UIImagePickerControllerSourceTypeSavedPhotosAlbum]; return; } [app showActionSheet]; } }
unity中调用方法
直接把这俩文件放在Plugins的iOS目录下,或者打包出Xcode工程后再导入这俩文件也可以,推荐第一种,做版本控制的时候比较方便
[DllImport("__Internal")]
private static extern void openPhoto();
public void OpenPhoto(string vName)
{
#if UNITY_IPHONE
openPhoto();
#endif
}
注:
imgData= UIImageJPEGRepresentation(image2, 0.5);是压缩文件 0.5是压缩质量
上传本地图片部分搞定!