CGImageでリサイズするメリット
手元のiPhone11で1920*1038画像を6000*6000にリサイズしました。
処理速度はCIImageが速いけれど、メモリ使用量はCGImageの方が少ない印象です。
ちなみに、画像の合成処理でもメモリ使用量が少なかったです。
format | time | memory(MB) |
---|---|---|
CGImage | 0.356 | 101 |
CIImage | 0.002 | 158.9 |
UIImage | 0.482 | 565 |
メモリ使用量はデバッガのメモリを目視しただけですが、
メモリ使用を抑えたいならCGImageを使うと良いと思います。
画質はUIImageが綺麗で、CIImageとCGImageは同じぐらいに見えました。
リサイズ方法
extension CGImage {
func resize(size:CGSize) -> CGImage? {
let width: Int = Int(size.width)
let height: Int = Int(size.height)
let bytesPerPixel = self.bitsPerPixel / self.bitsPerComponent
let destBytesPerRow = width * bytesPerPixel
guard let colorSpace = self.colorSpace else { return nil }
guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: self.bitsPerComponent, bytesPerRow: destBytesPerRow, space: colorSpace, bitmapInfo: self.alphaInfo.rawValue) else { return nil }
context.interpolationQuality = .high
context.draw(self, in: CGRect(x: 0, y: 0, width: width, height: height))
return context.makeImage()
}
}
🐣
フリーランスエンジニアです。
お仕事のご相談こちらまで
rockyshikoku@gmail.com
Core MLやARKitを使ったアプリを作っています。
機械学習/AR関連の情報を発信しています。