I'm scaling down a UIImage using this code:
let image = UIImage(contentsOfFile: imageUrl.path!)!.CGImage
let width = Float(CGImageGetWidth(image)) / 1.01
let height = Float(CGImageGetHeight(image)) / 1.01
let bitsPerComponent = CGImageGetBitsPerComponent(image)
let bytesPerRow = CGImageGetBytesPerRow(image)
let colorSpace = CGImageGetColorSpace(image)
let bitmapInfo = CGImageGetBitmapInfo(image)
let context = CGBitmapContextCreate(nil, Int(width), Int(height), bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo)
CGContextSetInterpolationQuality(context, kCGInterpolationHigh)
CGContextDrawImage(context, CGRect(origin: CGPointZero, size: CGSize(width: CGFloat(width), height: CGFloat(height))), image)
let scaledImage = UIImage(CGImage: CGBitmapContextCreateImage(context))
It's used in a Photo Extension. It works, scales the input image, but does not consider orientation. I keep orientation when editing starts:
func startContentEditingWithInput(contentEditingInput: PHContentEditingInput?, placeholderImage: UIImage) {
let output = PHContentEditingOutput(contentEditingInput: self.input)
let url = self.input?.fullSizeImageURL
if let imageUrl = url {
imageOrientation = input!.fullSizeImageOrientation
}
}
The saved output is always landscape.
This worked for me:
Mats' answer does work when I'm saving image.