Unity3d本地上傳並且裁剪圖片-----iOS平台

來源:互聯網
上載者:User

標籤:

註:引擎版本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 iPadif( 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 diskUIImage *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 popoverif( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && _popoverViewController ){[_popoverViewController dismissPopoverAnimated:YES];self.popoverViewController = nil;}}@endextern "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是壓縮品質


上傳本地圖片部分搞定!


Unity3d本地上傳並且裁剪圖片-----iOS平台

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.