【发布时间】:2020-08-16 11:57:06
【问题描述】:
我需要将两张图片合二为一,其中一张用作背景。我使用时的方法
let a = UIImage(named: "234")
let b = UIImage(named: "720")
let aRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
let bRect = CGRect(x: 0, y: 0, width: backgroundImg.size.width, height: backgroundImg.size.height)
...
let render = UIGraphicsImageRenderer(bounds: bRect)
let result = render.image { renderContext in
backgroundImg.draw(in: bRect)
image.draw(in: aRect)
}
return result
花了 2 毫秒。
但是当我使用时
let aPath = Bundle.main.path(forResource: "234@3x", ofType: "png")
let bPath = Bundle.main.path(forResource: "720@3x", ofType: "png")
let a = UIImage(contentsOfFile: aPath!)
let b = UIImage(contentsOfFile: bPath!)
...
let render = UIGraphicsImageRenderer(bounds: bRect)
let result = render.image { renderContext in
backgroundImg.draw(in: bRect)
image.draw(in: aRect)
}
return result
花了20ms,是什么导致时间增加了10倍?
我的困惑是,这两种方法都在图像组合之前将 a b 的两个图像加载到内存中。但是为什么图像合并的过程会有这样的差异呢?
这样的代码:
【问题讨论】:
标签: ios swift uikit core-graphics