2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CGImageでリサイズするメリット

手元のiPhone11で19201038画像を60006000にリサイズしました。

image9.jpeg

処理速度はCIImageが速いけれど、メモリ使用量はCGImageの方が少ない印象です。
ちなみに、画像の合成処理でもメモリ使用量が少なかったです。

|format|time|memory(MB)|
|---|---|---|---|
|CGImage |0.356 |101||
|CIImage |0.002| 158.9|
|UIImage |0.482 |565 |

メモリ使用量はデバッガのメモリを目視しただけですが、
メモリ使用を抑えたいならCGImageを使うと良いと思います。

画質はUIImageが綺麗で、CIImageとCGImageは同じぐらいに見えました。

CGImage
スクリーンショット 2022-02-10 4.21.57.png
CIImage
スクリーンショット 2022-02-10 4.22.10.png
UIImage
スクリーンショット 2022-02-10 4.22.20.png

リサイズ方法

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関連の情報を発信しています。

Twitter
Medium

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?