11
15

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.

【iOS, Swift】画像リサイズ処理の速度

Last updated at Posted at 2020-06-14

iOSDC 2019 発表資料 の下記にて画像リサイズの処理速度の検証が書かれていたので、自分でも検証してみました。
ただそれだけです。

画像処理におけるUIImageとCGImageとCIImageの効果的な使い分け

処理速度計測の準備

func measure <T> (_ f: @autoclosure () -> T) -> (result: T, duration: String) {
    let startTime = CFAbsoluteTimeGetCurrent()
    let result = f()
    let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
    return (result, "経過時間は \(timeElapsed) 秒です")
}

対象画像

1920 × 1280

sea-5237374_1920.jpg

計測対象

UIImageのリサイズ処理

extension UIImage {

    public func resize(to size: CGSize) -> UIImage {
        let format = UIGraphicsImageRendererFormat()
        format.scale = 1
        let renderer = UIGraphicsImageRenderer(size: size, format: format)
        
        let image = renderer.image {_ in
            let newRect = CGRect(origin: .zero, size: size)
            self.draw(in: newRect)
        }
        return image
   }
}

CIImageのリサイズ処理

extension CIImage {

   func resizeAffine(scaleX: CGFloat, scaleY: CGFloat) -> CIImage? {
         let matrix = CGAffineTransform(scaleX: scaleX, y: scaleY)
        return base.transformed(by: matrix)
    }

    func resizeLanczos(to scale: CGFloat) -> CIImage? {
        guard let filter = CIFilter(name: "CILanczosScaleTransform") else {
            return nil
        }
        
        filter.setDefaults()
        filter.setValue(self, forKey: kCIInputImageKey)
        filter.setValue(scale, forKey: kCIInputScaleKey)
        return filter.outputImage
   }
    
   func resizeBicubic(to scale: CGFloat) -> CIImage? {
         guard let filter = CIFilter(name: "CIBicubicScaleTransform") else {
             return nil
         }
         
         filter.setDefaults()
         filter.setValue(self, forKey: kCIInputImageKey)
         filter.setValue(scale, forKey: kCIInputScaleKey)
         return filter.outputImage
    }
}

計測

struct DemoImageResize {
    
    static func demoImageResize() {
        let image = UIImage(named: "sea.jpg")!
        let res1 = measure(image.ex.resize(to: CGSize(width: image.size.width*2, height: image.size.height*2)))
        print(res1.duration)
        
        let ciimage = CIImage(image: image)!
        let res2 = measure(ciimage.ex.resizeLanczos(to: 2))
        print(res2.duration)
        let res3 = measure(ciimage.ex.resizeBicubic(to: 2))
        print(res3.duration)
    }
}

結果

確かにCIImageを利用するほうが早く、特にアフィン変換が速い。

// UIImage
0.24390900135040283 秒
// CIImage (Affine) 
0.0000209808349609375 秒
// CIImage (Lanczos)
0.0028940439224243164 秒
// CIImage (Bicubic)
0.0014100074768066406 秒
11
15
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
11
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?