gpt4 book ai didi

ios - 在大型 UIView 上绘制占用大量内存

转载 作者:搜寻专家 更新时间:2023-11-01 06:14:19 27 4
gpt4 key购买 nike

当我开始在大型 UIView(宽度:3700,高度:40000)上绘制时,它会占用大量内存当应用程序启动时,内存是 150 MB,当开始在上面绘图时(调用 setNeedsDisplay 方法)大约需要 1 GB,应用程序会崩溃

class DrawingVc: UIViewController {

let contentView = DrawableView()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white

contentView.translatesAutoresizingMaskIntoConstraints = false
contentView.backgroundColor = .clear
self.view.addSubview(contentView)
contentView.frame = CGRect(x: 0, y: 0, width:view.frame.width, height:
view.frame.height * 50)
}

这里是自定义view的代码,可以看到,setNeedsDisplay运行在touchMoves

 class DrawableView: UIView {

var mLastPath: UIBezierPath?
weak var scdelegate: DrawableViewDelegate?
var isDrawEnable = true

private var drawingLines : [UIBezierPath] = []

override init(frame: CGRect) {
super.init(frame: frame)

}

required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

}


override func draw(_ rect: CGRect) {
debugPrint("request draw")

drawLine()

}

private func drawLine() {

UIColor.blue.setStroke()
for line in drawingList {
line.lineWidth = 4
line.stroke()
line.lineCapStyle = .round
}


}

var drawingList = [UIBezierPath]()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if touches.count == 2 {
return
}

let location = (touches.first?.location(in: self))!
mLastPath = UIBezierPath()
mLastPath?.move(to: location)
prevPoint = location

drawingList.append(mLastPath!)
}

var prevPoint: CGPoint?
var isFirst = true
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
debugPrint("touchesMoved: " , (touches.first?.location(in: self).x)! , (touches.first?.location(in: self).y)! )
if let coalescedtouches = event?.coalescedTouches(for: touches.first!)
{
for coalescedTouch in coalescedtouches
{
let locationInView = coalescedTouch.location(in: self)

if let prevPoint = prevPoint {
let midPoint = CGPoint( x: (locationInView.x + prevPoint.x) / 2, y: (locationInView.y + prevPoint.y) / 2)
if isFirst {
mLastPath?.addLine(to: midPoint)
}else {

mLastPath?.addQuadCurve(to: midPoint, controlPoint: prevPoint)
}
isFirst = false
} else {
mLastPath?.move(to: locationInView)

}
prevPoint = locationInView
}

}
setNeedsDisplay()
}



}

是什么导致了这个问题,如何解决?

最佳答案

您的 View 大于 iOS 设备上可能的最大屏幕,所以我想您的 View 嵌入在 ScrollView 中。您应该只绘制 View 的可见部分。不幸的是,UIView 不直接支持这一点。你可以看看CATiledLayer ,它支持仅绘制图层的可见部分,并且它也支持缩放图层的不同级别的细节。

关于ios - 在大型 UIView 上绘制占用大量内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48366057/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com