Unity3d upload and crop images locally-----iOS platform

Source: Internet
Author: User

Note: Engine version unity4.x

Recent project needs, need to do the user picture, requirements:

1. can upload from local

2. Local cropping

3. Compression control size

4. Implementation of Pc/android/ios on three platforms

I've been doing it for days.


On the Internet for a period of time, someone recommended a plugin, network address forgot to save, directly on the code bar

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:nsloc Alizedstring (@ "Cancel", nil) Destructivebuttontitle:nil Otherbuttontitles: Nslocalizedstring (@ "photo", nil), Nslocalizedstring (@ "album", Nil), Nil];if (ui_user_interface_idiom () = = Uiuserinterfaceid IOMPAD) [Sheet showfromrect:cgrectmake (0, 0, +) 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];p icker.delegate = Self;picker.sourcetype = type;picker.allowsediting = YES;//We Need to display the PopOver on Ipadif (ui_user_interface_idiom () = = Uiuserinterfaceidiompad) {Class Popoverclass = N        Sclassfromstring (@ "Uipopovercontroller"); if (!popoverclass) return; _popoverviewcontroller = [[Popoverclass alloc] initwithcontentviewcontroller:picker]; [_popoverviewcontroller setdelegate:self];//picker.modalinpopover = yes;//Display the popover[_  Popoverviewcontroller presentpopoverfromrect:cgrectmake (0, 0, 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 ();    Get the image, then use your function to return 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 has a popoverif (ui_user_interface_idiom () = = Uiuserinterfaceidiompad && _popoverviewcontroller) {[_popoverviewcontroller dismisspopoveranimated:yes]; Self.popoverviewcontroller = nil;}}        @endextern "C" {void Openphoto ()//album {//unitypause (TRUE);        Openphotocontroller * app = [[Openphotocontroller alloc] init]; No need to give a choice for devices with no camera if (![ Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {[App ShowPick            Er:uiimagepickercontrollersourcetypesavedphotosalbum];        Return            } [app Showactionsheet]; }    }



Calling Methods in Unity

Directly put these two files in the plugins iOS directory, or packaged out Xcode project and then import these two files can also, recommend the first, do version control time is more convenient

[DllImport ("__internal")]
private static extern void Openphoto ();

public void Openphoto (string vname)
{
#if Unity_iphone
Openphoto ();
#endif
}


Note:

Imgdata= uiimagejpegrepresentation (Image2, 0.5); is a compressed file 0.5 is a compression quality


Upload local image section to get it done!


Unity3d upload and crop images locally-----iOS platform

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.