Truncates the rectangular area in the center of the screen based on the size, and truncates the screen rectangle based on the size.

Source: Internet
Author: User

Truncates the rectangular area in the center of the screen based on the size, and truncates the screen rectangle based on the size.

Code:

-(Void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (50,100,200,200)]; imageView. backgroundColor = [UIColor redColor]; // display the captured image imageView. image = [self cutCenterImage: [UIImage imageNamed: @ "1.jpg"] size: CGSizeMake (200,200)]; [self. view addSubview: imageView] ;}# pragma mark captures the image in the rectangle area in the middle of the image Based on the size. The size here is a square-(UIImage *) cutCenterImage :( UIImage *) image size :( CGSize) size {CGSize imageSize = image. size; CGRect rect; // calculate the position and size of the rectangular area in the middle of the Image Based on the image size if (imageSize. width> imageSize. height) {float leftMargin = (imageSize. width-imageSize. height) * 0.5; rect = CGRectMake (leftMargin, 0, imageSize. height, imageSize. height);} else {float topMargin = (imageSize. height-imageSize. width) * 0.5; rect = CGRectMake (0, topMargin, imageSize. width, imageSize. width);} CGImageRef imageRef = image. CGImage; // intercept the middle area rectangular image CGImageRef imageRefRect = regular (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 an image tmp = UIGraphicsGetImageFromCurrentImageContext () after the size is changed from the current context (); // make the current context output stack UIGraphicsEndImageContext (); return tmp ;}

 

Related Article

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.