Code:
- (void) viewdidload{[Super Viewdidload]; //Do any additional setup after loading the view.Uiimageview*imageview=[[uiimageview Alloc]initwithframe:cgrectmake ( -, -, $, $)]; Imageview.backgroundcolor=[Uicolor Redcolor]; //show the captured graphImageview.image=[self cutcenterimage:[uiimage imagenamed:@"1.jpg"] Size:cgsizemake ( $, $)]; [Self.view Addsubview:imageview]; }#pragmaMark captures a picture of the middle rectangular area of a picture based on size the size here is square-(UIImage *) Cutcenterimage: (UIImage *) Image size: (cgsize) size{cgsize imageSize=image.size; CGRect rect; //calculates the position and size of the middle rectangular area of the image according to the size of the picture if(ImageSize.Width >imagesize.height) {floatLeftMargin = (imagesize.width-imagesize.height) *0.5; Rect= CGRectMake (LeftMargin,0, Imagesize.height, imagesize.height); }Else{ floatTopMargin = (imagesize.height-imagesize.width) *0.5; Rect= CGRectMake (0, TopMargin, ImageSize.Width, imagesize.width); } cgimageref imageref=image. Cgimage; //capturing a rectangular picture of the middle areaCgimageref Imagerefrect =Cgimagecreatewithimageinrect (Imageref, rect); UIImage*tmp =[[UIImage alloc] initwithcgimage:imagerefrect]; Cgimagerelease (Imagerefrect); Uigraphicsbeginimagecontext (size); CGRect Rectdraw= CGRectMake (0,0, Size.width, size.height); [TMP Drawinrect:rectdraw]; //create a resized picture from the current contextTMP =Uigraphicsgetimagefromcurrentimagecontext (); //make the current context out of the stackUigraphicsendimagecontext (); returntmp;}
Captures the middle rectangular area of the screen according to size