应用性能问题

时间:2017-12-20 06:51:48

标签: ios swift image performance

我正在做这些功能:

  1. 我正在通过Paint Bucket进行图像着色。

  2. zoomin用scrollview缩小图像。

  3. 合并这两个功能会使我的设备挂起一段时间。

    请检查以下代码工作并协助我。

    - 在imageView上点击func

      func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool{
    
            let touchView = gestureRecognizer.view
    
            if touchView == self.imgViewMy{
    
                let point : CGPoint = touch.location(in: imgViewMy)
                touchedPoint = point
    
               self.imgViewMy.image = self.imgDup
    
    
                // Doing asysn coloring in image to avoid device hang up.
                DispatchQueue.main.async( execute: {
    
                    print("touch is inside , \(self.touchedPoint!)")
    
               // Getting scaled coordinates
                    let aNewCor = self.getScaledPoint(self.touchedPoint!)
    
                  // Calling Paint Bucket function to change image color. 
                    self.imgDup = self.imgDup?.pbk_imageByReplacingColorAt(Int(aNewCor.x),
                                                                           Int(aNewCor.y),
                                                                           withColor: self.colorSel,
                                                                           tolerance: 10)
                    self.imgViewMy.image = self.imgDup
    
               })
           return false
        }
        return false
    }
    

    - 适用于图像放大&缩小

    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
         return imgViewMy
     }
    

1 个答案:

答案 0 :(得分:2)

尝试简化并进行一些清理:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool{
  let touchView = gestureRecognizer.view
  if touchView == self.imgViewMy {
    let point : CGPoint = touch.location(in: imgViewMy)
    touchedPoint = point
  }
  return true
}

func tapResponse(sender: UITapGestureRecognizer? = nil) {
  let aNewCor = self.getScaledPoint(self.touchedPoint!)
  self.imgViewMy.image = self.imgDup?.pbk_imageByReplacingColorAt(Int(aNewCor.x), Int(aNewCor.y), withColor: self.colorSel, tolerance: 5)
}
在设备上测试,似乎工作正常(它非常敏感)。