iOS for Avatar selection (photo or photo gallery), size, etc Zoom, create a circle avatar

Source: Internet
Author: User

//Eject Actionsheet. Choose how to get your avatar



//get pictures from albums-(void) Takepictureclick: (UIButton *) sender{//*/* NOTE: The following protocols are required for use: Uiimagepickercontrollerdelegate,//uinavigationcontrollerdelegate// *///Uiimagepickercontroller *picker = [[Uiimagepickercontroller alloc]init];// //set up a picture source (Photo album)//picker.sourcetype = uiimagepickercontrollersourcetypesavedphotosalbum;// //Set up proxy//picker.delegate = self;// //settings can be edited//picker.allowsediting = YES;// //Open the Picker interface//[self presentviewcontroller:picker animated:yes completion:nil];uiactionsheet* Actionsheet =[[Uiactionsheet alloc] Initwithtitle:@"Please select a file source" Delegate: Self cancelbuttontitle:@"Cancel"destructivebuttontitle:nil otherbuttontitles:@"Camera",@"Camera",@"Local Albums",@"Local Video", nil]; [Actionsheet ShowInView:self.view]; }#pragmaMark-#pragmaUiactionsheet delegate-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (nsinteger) buttonindex{NSLog (@"Buttonindex = [%d]", Buttonindex); Switch(buttonindex) { Case 0://Camera{Uiimagepickercontroller*imagepicker =[[Uiimagepickercontroller alloc] init]; Imagepicker.Delegate=Self ; Imagepicker.allowsediting=YES; Imagepicker.sourcetype=Uiimagepickercontrollersourcetypecamera;//[self presentmodalviewcontroller:imagepicker animated:yes];[self presentviewcontroller:imagepicker animated:yes completion:nil]; } Break; Case 1://Camera{Uiimagepickercontroller*imagepicker =[[Uiimagepickercontroller alloc] init]; Imagepicker.Delegate=Self ; Imagepicker.allowsediting=YES; Imagepicker.sourcetype=Uiimagepickercontrollersourcetypecamera; Imagepicker.videoquality=Uiimagepickercontrollerqualitytypelow;//[self presentmodalviewcontroller:imagepicker animated:yes];[self presentviewcontroller:imagepicker animated:yes completion:nil]; } Break; Case 2://Local Albums{Uiimagepickercontroller*imagepicker =[[Uiimagepickercontroller alloc] init]; Imagepicker.Delegate=Self ; Imagepicker.allowsediting=YES; Imagepicker.sourcetype=uiimagepickercontrollersourcetypephotolibrary;//[self presentmodalviewcontroller:imagepicker animated:yes];[self presentviewcontroller:imagepicker animated:yes completion:nil]; } Break; Case 3://Local Video{Uiimagepickercontroller*imagepicker =[[Uiimagepickercontroller alloc] init]; Imagepicker.Delegate=Self ; Imagepicker.allowsediting=YES; Imagepicker.sourcetype=uiimagepickercontrollersourcetypephotolibrary;//[self presentmodalviewcontroller:imagepicker animated:yes];[self presentviewcontroller:imagepicker animated:yes completion:nil]; } Break; default: Break; }}#pragmaMark-#pragmaUiimagepickercontroller delegate-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary *) info{if([[Info Objectforkey:uiimagepickercontrollermediatype] isequaltostring: (__bridge NSString *) Kuttypeimage]) {UIImage*img =[Info objectforkey:uiimagepickercontrollereditedimage]; [Self performselector: @selector (saveimage:) withobject:img afterdelay:0.5]; } Else if([[Info Objectforkey:uiimagepickercontrollermediatype] isequaltostring: (__bridge NSString *) Kuttypemovie]) {nsstring*videopath =[[Info objectforkey:uiimagepickercontrollermediaurl] path]; Self.filedata=[NSData Datawithcontentsoffile:videopath]; }//[Picker dismissmodalviewcontrolleranimated:yes];[Picker Dismissviewcontrolleranimated:yes completion:nil];}- (void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker{//[Picker dismissmodalviewcontrolleranimated:yes];[Picker Dismissviewcontrolleranimated:yes completion:nil];}- (void) SaveImage: (UIImage *) Image {//NSLog (@ "Save Avatar! "); //[Userphotobutton setimage:image forstate:uicontrolstatenormal];BOOL Success; Nsfilemanager*filemanager =[Nsfilemanager Defaultmanager]; Nserror*error; Nsarray*paths =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString*documentsdirectory = [Paths Objectatindex:0]; NSString*imagefilepath = [Documentsdirectory stringbyappendingpathcomponent:@"selfphoto.jpg"]; NSLog (@"imagefile->>%@", Imagefilepath); Success=[FileManager Fileexistsatpath:imagefilepath]; if(Success) {Success= [FileManager Removeitematpath:imagefilepath error:&ERROR]; }//UIImage *smallimage=[self scalefromimage:image tosize:cgsizemake (80.0f, 80.0f)];//change picture size to 80*80UIImage *smallimage = [Self thumbnailwithimagewithoutscale:image size:cgsizemake ( the, the)]; [UIImageJPEGRepresentation (Smallimage,1.0f) Writetofile:imagefilepath Atomically:yes];//Write FileUIImage *selfphoto = [UIImage Imagewithcontentsoffile:imagefilepath];//reading a picture file//[Userphotobutton Setimage:selfphoto forstate:uicontrolstatenormal];Self.img.image =Selfphoto;}//change the size of the image to easily upload the server-(UIImage *) Scalefromimage: (UIImage *) Image tosize: (cgsize) size{uigraphicsbeginimagecontext (size); [Image Drawinrect:cgrectmake (0,0, Size.width, Size.Height)]; UIImage*newimage =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); returnnewimage;}

2. Maintain the aspect ratio of the original image to create a picture of the desired size

//2. Maintain the original aspect ratio to generate a thumbnail image-(UIImage *) Thumbnailwithimagewithoutscale: (UIImage *) Image size: (cgsize) asize{UIImage*NewImage; if(Nil = =image) {NewImage=Nil; }    Else{cgsize oldsize=image.size;        CGRect rect; if(Asize.width/asize.height > oldsize.width/oldsize.height) {rect.size.width= asize.height*oldsize.width/Oldsize.height; Rect.size.height=Asize.height; Rect.origin.x= (asize.width-rect.size.width)/2; RECT.ORIGIN.Y=0; }        Else{rect.size.width=Asize.width; Rect.size.height= asize.width*oldsize.height/Oldsize.width; Rect.origin.x=0; RECT.ORIGIN.Y= (asize.height-rect.size.height)/2;        } uigraphicsbeginimagecontext (Asize); Cgcontextref Context=Uigraphicsgetcurrentcontext ();        Cgcontextsetfillcolorwithcolor (context, [[Uicolor Clearcolor] cgcolor]); Uirectfill (CGRectMake (0,0, Asize.width, asize.height));//Clear Background[Image Drawinrect:rect]; NewImage=Uigraphicsgetimagefromcurrentimagecontext ();    Uigraphicsendimagecontext (); }    returnnewimage;}

3. Show the Circle head

Nsarray *paths =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString*documentsdirectory = [Paths Objectatindex:0]; NSString*imagefilepath = [Documentsdirectory stringbyappendingpathcomponent:@"selfphoto.jpg"]; NSLog (@"imagefile->>%@", Imagefilepath); UIImage*selfphoto = [UIImage Imagewithcontentsoffile:imagefilepath];//Self.img.image =Selfphoto; [Self.img.layer setcornerradius:cgrectgetheight ([self.img bounds])/2]; Modify the radius to realize the circle of the Avatar Self.img.layer.masksToBounds= YES;

Hope to be helpful to everyone if you have a better way of achieving it. please tell me.

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.