Skip to content

Instantly share code, notes, and snippets.

@pczuchaj
pczuchaj / imageDownsampling
Created August 30, 2018 08:26
iOS App memory optimization - image downsampling technique
// Image downsampling technique provides smaller image buffer allocations which leads to memory optimization
extension UIImageView {
func downsampleImage(from imageUrl: URL) -> UIImage {
let imageSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary
guard let imageSource = CGImageSourceCreateWithURL(imageUrl as CFURL, imageSourceOptions) else { return UIImage() }
let maxDimensionInPixels = max(self.bounds.size.width, self.bounds.size.height) * self.traitCollection.displayScale
let downsampleOptions =
[kCGImageSourceCreateThumbnailFromImageAlways: true,