我确信这个问题已经从不同的角度被问过很多次了,但我目前还无法在这里找到答案。
我想要实现的目标 我想做的是显示一个 UIImage,并允许用户在图像上绘制一个矩形,并最终裁剪他们的选择。
迄今为止的研究 我在这里找到了之前关于处理裁剪的问题,但是它们通常处理不会改变的静态裁剪区域,这确实导致了这种机制的以下限制
- 可能会定位您感兴趣的图像区域 任何地方,例如,如果您尝试裁剪路标,它可能 位于一个图像的中心,但在另一张图像上向左对齐,因此您 无法预测要裁剪的区域。
- 感兴趣区域的大小和比例可能会发生变化,例如 一张图像可能是该路标的特写,因此裁剪区域 会更大,但另一幅图像可能是从 距离意味着裁剪区域会更小。
结合上述两个变量,几乎不可能准确预测图像中感兴趣的区域在哪里,因此我依靠用户通过能够在周围“绘制”一个框来定义它我们感兴趣的区域,在本例中是路标。
这在 Android 上是完美的,因为您可以带着良好的意图委托(delegate)所有艰苦的工作,例如:
Intent Intent = new Intent("com.android.camera.action.CROP");
但是,我找不到适用于 iOS 的同等产品。
我从这个 source 中找到了这段代码:
- (UIImage *)imageByDrawingCircleOnImage:(UIImage *)image
{
// begin a graphics context of sufficient size
UIGraphicsBeginImageContext(image.size);
// draw original image into the context
[image drawAtPoint:CGPointZero];
// get the context for CoreGraphics
CGContextRef ctx = UIGraphicsGetCurrentContext();
// set stroking color and draw circle
[[UIColor redColor] setStroke];
// make circle rect 5 px from border
CGRect circleRect = CGRectMake(0, 0,
image.size.width,
image.size.height);
circleRect = CGRectInset(circleRect, 5, 5);
// draw circle
CGContextStrokeEllipseInRect(ctx, circleRect);
// make image out of bitmap context
UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();
// free the context
UIGraphicsEndImageContext();
return retImage;
}
我认为这是一个很好的裁剪起点(但是它确实会产生麦田圈),它确实依赖于在调用 CGRectMake
时预定义要裁剪的区域。
这个previous question还详细说明了如何进行实际裁剪。
我假设为了允许用户绘制矩形,我需要与手势集成?
问题: 如何允许用户在 ImageView 上绘制矩形,以裁剪该区域?
最佳答案
你可以给BJImageCropper尝试一下:
A simple UIView subclass that allows a user to crop an image. If you use it, I'd love to know! Twitter: @barrettjacobsen
关于ios - 允许用户在 UIImage 上绘制矩形,以裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10849330/