26

Is there any cropping image API for objective C that crops images dynamically in Xcode project? Please provide some tricks or techniques how could I crop camera images in iPhone.

j0k
  • 22,600
  • 28
  • 79
  • 90
Appz Venture
  • 939
  • 1
  • 13
  • 28

4 Answers4

18

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;
}
Geek
  • 8,280
  • 17
  • 73
  • 137
10

EDIT - Swift Version

let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFill
imageView.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.

sf89
  • 5,088
  • 7
  • 24
  • 27
  • I think that your answer simply resizes the image temporarily within the bounds of the `UIImageView`. If you are only concerned with displaying the image, this will work. However, if, for example, you need to modify the image before uploading to a server, this approach would not work in that case. – Andrew Oct 27 '15 at 20:05
  • True but she/he is looking for a way that "crops images dynamically", so my suggestion is perfectly fine here. – sf89 Oct 29 '15 at 06:06
1

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]]; 
} 
kleopatra
  • 51,061
  • 28
  • 99
  • 211
1

Xcode 5, iOS 7, and 4-inch screen example: Here is an open source example of a SimpleImageCropEditor (Project Zip and Source Code Example. You can load the Image Crop Editor as a Modal View Controller and reuse. Look at the code and please leave constructive comments concerning if this example code answers the question "Image Cropping API for iOS".

Demonstrates, is example source Objective-C code, use of UIImagePickerController, @protocol, UIActionSheet, UIScrollView, UINavigationController, MFMailComposeViewController, and UIGestureRecognizer.