开发者

Image Cropping API for iOS

开发者 https://www.devze.com 2023-03-29 02:35 出处:网络
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.You c

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.


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 = .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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消