Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image Cropping API for iOS

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.

like image 528
Appz Venture Avatar asked Aug 17 '11 02:08

Appz Venture


3 Answers

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;
}
like image 180
Geek Avatar answered Oct 12 '22 00:10

Geek


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.

like image 23
sf89 Avatar answered Oct 11 '22 23:10

sf89


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]]; 
} 
like image 34
user2963866 Avatar answered Oct 12 '22 00:10

user2963866