Captures the middle rectangular area of the screen according to size

Source: Internet
Author: User

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

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.