我正试图做一个应用程序,我可以从图片中删除背景,用户从导入“相机胶卷”。
所以我想在UIImage上画一个UIColor.clearColor。 所以我尝试使用核心图形来绘制我的UIImage。
我试图最初由画线:在Swift上擦除图片的最佳方法是什么?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
lastPoint = touch.locationInView(self)
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
var newPoint = touch.locationInView(self)
lines.append(Line(start: lastPoint, end: newPoint))
lastPoint = newPoint
self.setNeedsDisplay()
}
}
override func drawRect(rect: CGRect) {
imageToSend.drawAtPoint(CGPointZero)
var context = UIGraphicsGetCurrentContext()
CGContextBeginPath(context)
for line in lines {
CGContextMoveToPoint(context, line.start.x, line.start.y)
CGContextAddLineToPoint(context, line.end.x, line.end.y)
}
CGContextSetStrokeColorWithColor(context, UIColor.redColor().CGColor) //tried with a red color here
CGContextSetLineWidth(context, 80.0)
CGContextSetLineCap(context, .Round)
}
的问题是,这是非常laggy。调试会话(的xCode)的内存使用率非常高,以及CPU使用率。
在iPhone 6s上运行应用程序。
那么是否有更好的方法来擦除Swift中的图片?
另一个问题是,它创建线条,我希望它更平滑。但这是另一个问题
我的假设是性能滞后是由* drawRect *方法的频繁调用引起的。你在那里做了非常大的工作 - 渲染图像。考虑摆脱以下内容:* imageToSend.drawAtPoint(CGPointZero)*。尝试评论一下,看看表现是否变得更好。如果有帮助,可以尝试使用自定义视图与图库中的图像叠加UIImageView,该自定义视图在其上进行绘制。这是一个有趣的问题,如果你分享了一些示例代码,我会玩。 –
@Melifaro是的,它变得更好。所以我应该把我的DrawingView放在UIImageView之上?那我怎么擦除图像呢?谢谢你的帮助 – Danny182