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 ;}