2017-04-14 109 views
0

我試圖讓橡皮擦,我寫了這個:橡皮擦工具爲iOS應用

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

我可以建議你的唯一一件事 - 降低圖像的質量/尺寸。這對我有效。

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() 
    } 
}