iOS 图片编辑开源库——或者只是一些小技巧

标签 ios drawing uiimage core-graphics editing

对于我正在编写的 ios 应用程序,我想从照片库中拍摄一张照片,然后让用户“清理它”,基本上删除不需要的部分。例如,假设用户选择了一张人的照片,我的应用程序只需要头部,其他所有内容都应该删除,因此用户需要通过删除背景、 body 或照片中的其他人来清理照片。想象一下类似 Photoshop 的体验,但只有一个工具——橡皮擦。

我正在寻找开源库、示例或只是关于如何开始使用的提示。

我知道如何使用 UIImagePickerController 选择图像,因此缺少的部分是实际的编辑。作为一个完全的菜鸟,我很乐意就什么是合理的方法获得一些建议,最好是一些示例代码,甚至是可重用的库。

我想,在高层次上,我想做的是从一个矩形图像开始,并确保它有一个 alpha 层,然后当用户触摸图像的某些部分以删除它们时,我需要“删除”通过将它们的 alpha 级别更改为 0 从图像中获得更多像素。但这是一个太高级别的描述,我什至不确定是否正确......另一个合理的要求是撤消支持。

想到的另一种方法是使用原始图像和用户在触摸屏幕时编辑的蒙版图像,当“完成”时,以某种方式将两个图像编译为一个具有 alpha 的图像。当然,这是一个实现细节,用户不需要知道屏幕上有两个图像。

如果可能,我希望停留在 UIImage 或 UIImageView 或 Core Graphics 级别,而不必与 OpenGL ES 混为一谈。我的直觉是,更高的图形级别应该具有足够的性能并且易于理解,可维护的干净代码是一个考虑因素......

感谢任何建议,谢谢!

最佳答案

事实证明这很容易,感谢@Rog 的指点。
我将在下面粘贴我的解决方案。这进入 Controller 代码:

#pragma mark - touches

- (void) clipImageCircle:(CGPoint)point radius:(CGFloat)radius {
  UIBezierPath* uiBezierPath = [UIBezierPath  bezierPathWithArcCenter:point radius:radius startAngle:0 endAngle:2 * M_PI clockwise:NO];
  CGPathRef erasePath = uiBezierPath.CGPath;
  UIImage *img = imageView.image;
  CGSize s = img.size;
  UIGraphicsBeginImageContext(s);
  CGContextRef g = UIGraphicsGetCurrentContext();
  CGContextAddPath(g, erasePath);
  CGContextAddRect(g,CGRectMake(0, 0, s.width, s.height));
  CGContextEOClip(g);
  [img drawAtPoint:CGPointZero];
  imageView.image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
}

- (void) receiveTouch:(CGPoint)point {
  NSLog(@"%@", NSStringFromCGPoint(point));
  [self clipImageCircle:point radius:20];
}

- (void) endTouch {
  NSLog(@"END TOUCH");
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  // We currently support only single touch events
  UITouch* touch = [touches anyObject];
  CGPoint point = [touch locationInView:imageView];
  if ([imageView hitTest:point withEvent:event]) {
    [self receiveTouch:point];
  }
}

- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
  [self endTouch];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  [self endTouch];
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  UITouch* touch = [touches anyObject];
  CGPoint point = [touch locationInView:imageView];
  if ([imageView hitTest:point withEvent:event]) {
    [self receiveTouch:point];
  }
}

关于iOS 图片编辑开源库——或者只是一些小技巧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6436160/

相关文章:

ios - 使用 Storyboard时如何从应用程序委托(delegate)中获取可见的 viewController?

ios - 从 swift 2.2 中的 plist 的字典和数组的组合中获取数据

ios - Xcode:UIPickerView 文本和 View Controller 链接

c# - 如何在单击按钮时绘制矩形?

c++ - Qt - 更新 PaintGL

objective-c - 删除由 NSRectFill 完成的 cocoa 绘图?

iphone - 在imageview中的图像透明部分着色

ios - 如何在 swift 4 中将角标(Badge)放在 UIBarButtonItem 上?

ios - *** 错误 *** CGImageSource 创建时数据大小为 : 22467 - current size is only: 6139

ios - UIImage 性能 : imageNamed vs UIGraphicsGetImageFromCurrentImageContext from bezierPath