Image Cropping API for iOS
You can use below simple code to crop an image. You have to pass the image and the CGRect which is the cropping area. Here, I crop image so that I get center part of original image and returned image is square.
// Returns largest possible centered cropped image.- (UIImage *)centerCropImage:(UIImage *)image{ // Use smallest side length as crop square length CGFloat squareLength = MIN(image.size.width, image.size.height); // Center the crop area CGRect clippedRect = CGRectMake((image.size.width - squareLength) / 2, (image.size.height - squareLength) / 2, squareLength, squareLength); // Crop logic CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect); UIImage * croppedImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); return croppedImage;}
EDIT - Swift Version
let imageView = UIImageView(image: image)imageView.contentMode = .scaleAspectFillimageView.clipsToBounds = true
All these solutions seem quite complicated and many of them actually degrade the quality the image.You can do much simpler using UIImageView
's out of the box methods.
Objective-C
self.imageView.contentMode = UIViewContentModeScaleAspectFill;[self.imageView setClipsToBounds:YES];[self.imageView setImage:img];
This will crop your image based on the dimensions you've set for your UIImageView
(I've called mine imageView
here).
It's that simple and works much better than the other solutions.
You can use CoreGraphics framework to cropping image dynamically. Here is a example code part of dynamic image crop. I hope this will be helpful for you.
- (void)drawMaskLineSegmentTo:(CGPoint)ptTo withMaskWidth:(CGFloat)maskWidth inContext:(NXMaskDrawContext)context{ if (context == nil) return; if (context.count <= 0){ [context addObject:[NSValue valueWithCGPoint:ptTo]]; return; } CGPoint ptFrom = [context.lastObject CGPointValue]; UIGraphicsBeginImageContext(self.maskImage.size); [self.maskImage drawInRect:CGRectMake(0, 0, self.maskImage.size.width, self.maskImage.size.height)]; CGContextRef graphicsContext = UIGraphicsGetCurrentContext(); CGContextSetBlendMode(graphicsContext, kCGBlendModeCopy); CGContextSetRGBStrokeColor(graphicsContext, 1, 1, 1, 1); CGContextSetLineWidth(graphicsContext, maskWidth); CGContextSetLineCap(graphicsContext, kCGLineCapRound); CGContextMoveToPoint(graphicsContext, ptFrom.x, ptFrom.y); CGContextAddLineToPoint(graphicsContext, ptTo.x, ptTo.y); CGContextStrokePath(graphicsContext); self.maskImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIGraphicsBeginImageContext(self.displayableMaskImage.size); [self.displayableMaskImage drawInRect:CGRectMake(0, 0, self.displayableMaskImage.size.width, self.displayableMaskImage.size.height)]; graphicsContext = UIGraphicsGetCurrentContext(); CGContextSetBlendMode(graphicsContext, kCGBlendModeCopy); CGContextSetStrokeColorWithColor(graphicsContext, self.displayableMaskColor.CGColor); CGContextSetLineWidth(graphicsContext, maskWidth); CGContextSetLineCap(graphicsContext, kCGLineCapRound); CGContextMoveToPoint(graphicsContext, ptFrom.x, ptFrom.y); CGContextAddLineToPoint(graphicsContext, ptTo.x, ptTo.y); CGContextStrokePath(graphicsContext); self.displayableMaskImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [context addObject:[NSValue valueWithCGPoint:ptTo]]; }