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

Ios 这是在UIImageview上绘制的正确方法吗,ios,swift,uiimageview,Ios,Swift,Uiimageview,我正试着做一个应用程序,它会随着我的手指在屏幕上画画。我从几个网站上取了一些作品,并设法完成了 该应用程序正在工作,我可以选择不同的颜色和线是按照我的手指。唯一的问题是,当我在一个真正的设备(iphone SE和Xs Max)上使用它时,我的应用程序在一堆行之后崩溃,并显示消息iOS terminate on memory。 这种情况不会发生在模拟器上。我没有足够的经验使用仪器。你知道这是什么原因吗 可能函数中有问题: func drawLineFrom(fromPoint:CGPoint,to

我正试着做一个应用程序,它会随着我的手指在屏幕上画画。我从几个网站上取了一些作品,并设法完成了

该应用程序正在工作,我可以选择不同的颜色和线是按照我的手指。唯一的问题是,当我在一个真正的设备(iphone SE和Xs Max)上使用它时,我的应用程序在一堆行之后崩溃,并显示消息iOS terminate on memory。 这种情况不会发生在模拟器上。我没有足够的经验使用仪器。你知道这是什么原因吗

可能函数中有问题:

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

        }

    }
导入UIKit
类ViewController:UIViewController{
@ibvar-UIImage:UIImageView!
@IBVAR tempImageView:UIImageView!
@IBOUTLE弱var颜色使用:UILabel!
var lastPoint=CGPoint.zero
变量红色:CGFloat=0.0
绿色变量:CGFloat=0.0
蓝色变量:CGFloat=0.0
变量刷宽:CGFloat=2.0
变量不透明度:CGFloat=1.0
var swiped=false
让颜色:[(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),
]
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
UIImage.isUserInteractionEnabled=true
tempImageView.isUserInteractionEnabled=true
colorinuse.text=“黑色”
}
@iAction func pencilPressed(发件人:AnyObject){
var index=sender.tag±0
如果索引<0 | |索引>=colors.count{
索引=0
}
(红、绿、蓝)=颜色[索引]
如果索引==colors.count-1{
不透明度=1.0
}
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
刷卡=错误
如果让触摸=先触摸{
lastPoint=touch.location(在:self.view中)
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
刷卡=正确
如果让触摸=先触摸{
让currentPoint=触摸位置(在:视图中)
drawLineFrom(起点:最后一点,拓扑点:当前点)
lastPoint=currentPoint
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
如果!刷卡{
drawLineFrom(起点:最后一点,地形点:最后一点)
}
//将tempImageView合并到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,宽度:view.frame.size.width,高度:view.frame.size.height),blendMode:CGBlendMode.normal,alpha:opacity)
UIImage.image=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
tempImageView.image=nil
}
func drawLineFrom(起点:CGPoint,地形点:CGPoint){
UIGraphicsBeginImageContext(view.frame.size)
UIGraphicsBeginImageContextWithOptions(view.frame.size,false,0)
让drawContext=UIGraphicsGetCurrentContext()
tempImageView.image?.draw(in:CGRect(x:0,y:0,width:view.frame.size.width,height:view.frame.size.height))
drawContext?移动(到:CGPoint(x:fromPoint.x,y:fromPoint.y))
drawContext?.addLine(到:CGPoint(x:toPoint.x,y:toPoint.y))
drawContext?.setLineCap(CGLineCap.round)
drawContext?.setLineWidth(画笔宽度)
drawContext?.setStrokeColor(红色:红色,绿色:绿色,蓝色:蓝色,alpha:1.0)
drawContext?.setBlendMode(CGBlendMode.normal)
drawContext?.strokePath()
tempImageView.image=UIGraphicsGetImageFromCurrentImageContext()
tempImageView.alpha=不透明度
UIGraphicsSendImageContext()
}
}

尝试在CAShapeLayer中使用自定义UIView子类。@Cosmos Thx我从您的解决方案开始,发现了一个很好的教程:另一个技巧和窍门是,您应该在绘制代码时少做一些工作。在这里,您在16毫秒的时间范围内做了很多工作。如果您想了解更多,可以搜索石英二维编程指南"和相关的CoreGraphics文章。@Cosmos-Thx我也会研究它,但今天时间不多了。@Cosmos…我设法让它在不崩溃的情况下工作,并且我的内存保持较低。尝试使用CAShapeLayer的自定义UIView子类。@Cosmos-Thx我从您的解决方案开始,找到了一个很好的教程:另一个技巧和窍门是您应该在绘图代码方面,您的工作量会减少。在这里,您在16毫秒的时间范围内完成了大量工作。如果您想了解更多信息,可以搜索“Quartz 2D编程指南”和相关的CoreGraphics文章。@Cosmos Thx我也会研究它,但今天时间不多了。@Cosmos…我成功地让它工作而没有崩溃,我的记忆保持低