我正在尝试制作橡皮擦工具,我写了这个:
class EraserTool: Tool {
var context: CGContext?
fileprivate unowned let delegate: ToolDelegate
fileprivate unowned let imageView: UIImageView
fileprivate var currentPoint = CGPoint()
required init(imageView: UIImageView, delegate: ToolDelegate) {
self.delegate = delegate
self.imageView = imageView
}
func toucheBegin(at point: CGPoint) {
guard let image = imageView.image else {
printErr("can't erase, image nil")
return
}
UIGraphicsBeginImageContext(image.size)
guard let context = UIGraphicsGetCurrentContext() else {
printErr("can't get current context")
return
}
context.setBlendMode(.clear)
context.setLineCap(.round)
context.setLineWidth(CGFloat(delegate.toolWidthPercent) / imageView.imageScale * 100)
self.context = context
delegate.saveImageBeforeEditing()
currentPoint = point
}
func toucheMoved(to point: CGPoint) {
guard let context = context else {
printErr("no context")
return
}
imageView.erase(from: currentPoint, to: point, context: context)
currentPoint = point
}
func toucheEnd(at point: CGPoint) {
guard let context = context else {return}
imageView.erase(from: currentPoint, to: point, context: context)
UIGraphicsEndImageContext()
}
}
extension UIImageView {
func erase(from startPoint: CGPoint, to endPoint: CGPoint, context: CGContext) {
guard let image = image else {
printErr("can't erase, image nil")
return
}
image.draw(at: CGPoint())
context.beginPath()
context.move(to: startPoint)
context.addLine(to: endPoint)
context.strokePath()
self.image = UIGraphicsGetImageFromCurrentImageContext()
}
}
它可以很好地用于小图像,但是当我为较大的图像运行它时 - 它的处理速度非常慢。难道我做错了什么?有哪些方法可以提高效率?
答案 0 :(得分:0)
我唯一可以建议你 - 降低图像的质量/尺寸。这对我有用。
extension UIImage {
func resizedToScreenDimensionsCopy() -> UIImage? {
let screenScale = UIScreen.main.scale
let ratio = (size.width * size.height) / (UIScreen.main.bounds.width * screenScale * UIScreen.main.bounds.height * screenScale * 3)
if ratio < 1 {
return self
}
let newImageSize = CGSize(width: size.width / ratio, height: size.height / ratio)
UIGraphicsBeginImageContext(newImageSize)
defer {
UIGraphicsEndImageContext()
}
draw(in: CGRect(origin: CGPoint(), size: newImageSize))
return UIGraphicsGetImageFromCurrentImageContext()
}
}