【发布时间】:2011-10-28 13:58:46
【问题描述】:
是否有任何用于在 Xcode 项目中动态裁剪图像的目标 C 裁剪图像 API?请提供一些技巧或技术如何在 iPhone 中裁剪相机图像。
【问题讨论】:
标签: objective-c image crop
是否有任何用于在 Xcode 项目中动态裁剪图像的目标 C 裁剪图像 API?请提供一些技巧或技术如何在 iPhone 中裁剪相机图像。
【问题讨论】:
标签: objective-c image crop
您可以使用下面的简单代码来裁剪图像。您必须传递图像和作为裁剪区域的 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;
}
【讨论】:
编辑 - 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)。
就是这么简单,而且比其他解决方案效果更好。
【讨论】:
UIImageView 的范围内临时调整图像大小。如果您只关心显示图像,这将起作用。但是,例如,如果您需要在上传到服务器之前修改图像,则这种方法在这种情况下不起作用。
您可以使用 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]];
}
【讨论】:
Xcode 5、iOS 7 和 4 英寸屏幕示例:这是一个开源示例 SimpleImageCropEditor (Project Zip and Source Code Example。您可以将图像裁剪编辑器加载为模态视图控制器并重复使用。查看代码,如果此示例代码回答了“iOS 图像裁剪 API”问题,请留下建设性的 cmets。
演示,是示例源 Objective-C 代码,使用 UIImagePickerController、@protocol、UIActionSheet、UIScrollView、UINavigationController、MFMailComposeViewController 和 UIGestureRecognizer。
【讨论】: