ios - 允许用户在 UIImage 上绘制矩形,以裁剪图像

标签 ios ios5 uiimage crop

我确信这个问题已经从不同的角度被问过很多次了,但我目前还无法在这里找到答案。

我想要实现的目标 我想做的是显示一个 UIImage,并允许用户在图像上绘制一个矩形,并最终裁剪他们的选择。

迄今为止的研究 我在这里找到了之前关于处理裁剪的问题,但是它们通常处理不会改变的静态裁剪区域,这确实导致了这种机制的以下限制

  1. 可能会定位您感兴趣的图像区域 任何地方,例如,如果您尝试裁剪路标,它可能 位于一个图像的中心,但在另一张图像上向左对齐,因此您 无法预测要裁剪的区域。
  2. 感兴趣区域的大小和比例可能会发生变化,例如 一张图像可能是该路标的特写,因此裁剪区域 会更大,但另一幅图像可能是从 距离意味着裁剪区域会更小。

结合上述两个变量,几乎不可能准确预测图像中感兴趣的区域在哪里,因此我依靠用户通过能够在周围“绘制”一个框来定义它我们感兴趣的区域,在本例中是路标。

这在 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/

相关文章:

iphone - 如何使用 iOS 的 api 1.1 获取 Twitter 个人资料图片?

ios - ObjectBinding 和 EnvironmentObject 有什么区别?

ios - 导航栏 Obj-C 中的 Logo

iphone - 设备选择了错误的视网膜图像

iphone - 如何在 ARToolKitPlus 中使用 UIImage? (UIImage 到 CVImageBufferRef 的转换)

iphone - iOS 5 中 UINavigationBar 中的截止标题标签

ios5 - MPMoviePlayerController 覆盖控件在重用后不响应触摸

iphone - UIAlertViewDelegate 类 "self"实例在按下按钮之前被释放

ios5 - 使用 Storyboard 自定义 UITableViewCell

ios - MPNowPlayingInfoCenter nowPlayingInfo 通过 AirPlay 忽略了 AVPlayer 音频