|
@@ -106,4 +106,27 @@ extension ImageFormat {
|
|
|
return nil
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // This scaling method is more memory efficient than UIImage.scaleDownImage(toMax: CGFloat)
|
|
|
+ // but requires an NSURL as parameter
|
|
|
+ public static func scaleDownImage(_ url: NSURL, toMax: CGFloat) -> UIImage? {
|
|
|
+ let imgSource = CGImageSourceCreateWithURL(url, nil)
|
|
|
+ guard let imageSource = imgSource else {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ var scaledImage: CGImage?
|
|
|
+ let options: [NSString: Any] = [
|
|
|
+ // The maximum width and height in pixels of a thumbnail.
|
|
|
+ kCGImageSourceThumbnailMaxPixelSize: toMax,
|
|
|
+ kCGImageSourceCreateThumbnailFromImageAlways: true,
|
|
|
+ // Should include kCGImageSourceCreateThumbnailWithTransform: true in the options dictionary. Otherwise, the image result will appear rotated when an image is taken from camera in the portrait orientation.
|
|
|
+ kCGImageSourceCreateThumbnailWithTransform: true
|
|
|
+ ]
|
|
|
+ scaledImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options as CFDictionary)
|
|
|
+ if let scaledImage = scaledImage {
|
|
|
+ return UIImage(cgImage: scaledImage)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ }
|
|
|
}
|