适用于 iOS 的图像裁剪 API

发布于 2024-11-30 02:44:23 字数 83 浏览 4 评论 0原文

Objective C 是否有任何裁剪图像 API 可以在 Xcode 项目中动态裁剪图像?请提供一些如何在 iPhone 中裁剪相机图像的技巧或技巧。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

长不大的小祸害 2024-12-07 02:44:23

您可以使用下面的简单代码来裁剪图像。您必须传递图像和 CGRect(即裁剪区域)。在这里,我裁剪图像,以便获得原始图像的中心部分,并且返回的图像是正方形的。

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

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;
}
深巷少女 2024-12-07 02:44:23

编辑 - Swift 版本

let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true

所有这些解决方案看起来都相当复杂,其中许多实际上降低了图像质量。
您可以使用 UIImageView 的开箱即用方法来简化操作。

Objective-C

self.imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.imageView setClipsToBounds:YES];
[self.imageView setImage:img];

这将根据您为 UIImageView 设置的尺寸(我在此处称为我的 imageView)裁剪您的图像。

就是这么简单,而且比其他解决方案效果更好。

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.

绝不服输 2024-12-07 02:44:23

您可以使用 CoreGraphics 框架动态裁剪图像。
这是动态图像裁剪的示例代码部分。我希望这对您有帮助。

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

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]]; 
} 
高跟鞋的旋律 2024-12-07 02:44:23

Xcode 5、iOS 7 和 4 英寸屏幕示例:这是一个开源示例
SimpleImageCropEditor(项目压缩包和源代码示例)。您可以将图像裁剪编辑器作为模态视图控制器加载查看代码,请留下关于此示例代码是否回答问题“iOS 的图像裁剪 API”的建设性评论,

这是示例源 Objective-C 代码,使用。 UIImagePickerController、@protocol、UIActionSheet、UIScrollView、UINavigationController、MFMailComposeViewController 和 UIGestureRecognizer。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文