这是在UIImageview上绘制的正确方法

时间:2019-11-05 07:23:24

标签: ios swift uiimageview

我正在尝试制作一个可以顺着手指在屏幕上绘制的应用程序。我从几个网站上收集了资料,并设法完成了。 screenshot app screen

该应用程序正在运行,我可以选择不同的颜色,并且线条沿我的手指延伸。唯一的事情是,当我在一排线后在真实设备(iPhone SE和Xs Max)上使用它时,我的应用程序崩溃并显示消息iOS终止在内存中。 模拟器不会发生这种情况。我没有足够的经验来使用乐器。知道是什么原因造成的吗?

函数可能有问题:

func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {

或者iPhone的内存不足以在全屏上绘制,那么我在浪费时间寻找没有解决方案的解决方案吗?

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var UIImage: UIImageView!
    @IBOutlet weak var tempImageView: UIImageView!

    @IBOutlet weak var colourInUse: UILabel!
    var lastPoint = CGPoint.zero
     var red: CGFloat = 0.0
     var green: CGFloat = 0.0
     var blue: CGFloat = 0.0
     var brushWidth: CGFloat = 2.0
     var opacity: CGFloat = 1.0
     var swiped = false

     let colors: [(CGFloat, CGFloat, CGFloat)] = [
       (0, 0, 0),
       (105.0 / 255.0, 105.0 / 255.0, 105.0 / 255.0),
       (1.0, 0, 0),
       (0, 0, 1.0),
       (51.0 / 255.0, 204.0 / 255.0, 1.0),
       (102.0 / 255.0, 204.0 / 255.0, 0),
       (102.0 / 255.0, 1.0, 0),
       (160.0 / 255.0, 82.0 / 255.0, 45.0 / 255.0),
       (1.0, 102.0 / 255.0, 0),
       (1.0, 1.0, 0),
       (1.0, 1.0, 1.0),
     ]



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        UIImage.isUserInteractionEnabled = true
        tempImageView.isUserInteractionEnabled = true
        colourInUse.text = "Black"
    }


    @IBAction func pencilPressed(sender: AnyObject) {

      var index = sender.tag ?? 0
      if index < 0 || index >= colors.count {
        index = 0
      }

      (red, green, blue) = colors[index]

      if index == colors.count - 1 {
        opacity = 1.0
      }


      }


      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
          swiped = false
        if let touch = touches.first {
                lastPoint = touch.location(in: self.view)
             }
      }

     override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
         swiped = true
        if let touch = touches.first {
            let currentPoint = touch.location(in: view)
            drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint)
             lastPoint = currentPoint
        }
    }

        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

            if !swiped {
            drawLineFrom(fromPoint: lastPoint, toPoint: lastPoint)
          }

          // Merge tempImageView into mainImageView
          UIGraphicsBeginImageContext(UIImage.frame.size)
            UIImage.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: CGBlendMode.normal, alpha: 1.0)
            tempImageView.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: CGBlendMode.normal, alpha: opacity)
          UIImage.image = UIGraphicsGetImageFromCurrentImageContext()
          UIGraphicsEndImageContext()

          tempImageView.image = nil
        }





        func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {

          UIGraphicsBeginImageContext(view.frame.size)
            UIGraphicsBeginImageContextWithOptions(view.frame.size, false, 0)
          let drawContext = UIGraphicsGetCurrentContext()
            tempImageView.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))

            drawContext?.move(to: CGPoint(x: fromPoint.x, y: fromPoint.y))
            drawContext?.addLine(to: CGPoint(x: toPoint.x, y: toPoint.y))

            drawContext?.setLineCap(CGLineCap.round)
            drawContext?.setLineWidth(brushWidth)

            drawContext?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
            drawContext?.setBlendMode(CGBlendMode.normal)
            drawContext?.strokePath()

            tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
          tempImageView.alpha = opacity

          UIGraphicsEndImageContext()

        }

    }

0 个答案:

没有答案