我正在使用此代码缩小 UIImage :
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))
它用于照片扩展。它可以工作,缩放输入图像,但不考虑方向。编辑开始时我会保持方向:
func startContentEditingWithInput(contentEditingInput: PHContentEditingInput?, placeholderImage: UIImage) {
let output = PHContentEditingOutput(contentEditingInput: self.input)
let url = self.input?.fullSizeImageURL
if let imageUrl = url {
imageOrientation = input!.fullSizeImageOrientation
}
}
保存的输出始终为横向。
答案 0 :(得分:0)
最简单的解决方案是在创建UIImage输出时重新使用UIImage输入的图像方向。
let uiImage = UIImage(contentsOfFile: imageUrl.path!)
let image = uiImage!.CGImage
// ...
let scaledImage = UIImage(CGImage: CGBitmapContextCreateImage(context),
scale: uiImage.scale,
orientation: uiImage.imageOrientation)
答案 1 :(得分:0)
这对我有用:
let scaledImage = UIImage(CGImage: CGBitmapContextCreateImage(context))
var cimage: CIImage
cimage = CIImage(image: scaledImage)
if self.imageOrientation != nil {
cimage = cimage.imageByApplyingOrientation(self.imageOrientation!)
}
var _context = CIContext(options: nil)
var cgImage = _context.createCGImage(cimage,
fromRect: cimage.extent())
var resultImage = UIImage(CGImage: cgImage)
垫'当我保存图像时,答案确实有效。