articles
articles copied to clipboard
Image resizing using CGImageSourceCreateThumbnailAtIndex should maintain rotation
Article: https://github.com/NSHipster/articles/blob/master/2014-09-15-image-resizing.md
import ImageIO
if let imageSource = CGImageSourceCreateWithURL(self.URL, nil) {
let options: [NSString: NSObject] = [
kCGImageSourceThumbnailMaxPixelSize: max(size.width, size.height) / 2.0,
kCGImageSourceCreateThumbnailFromImageAlways: true
]
let scaledImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options).flatMap { UIImage(CGImage: $0) }
}
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.
import ImageIO
if let imageSource = CGImageSourceCreateWithURL(self.URL, nil) {
let options: [NSString: NSObject] = [
kCGImageSourceThumbnailMaxPixelSize: max(size.width, size.height) / 2.0,
kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceCreateThumbnailWithTransform: true
]
let scaledImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options).flatMap { UIImage(CGImage: $0) }
}
Thank you for posting this; I was having the problem that your fix fixes.