裁剪后 UIImage 旋转 90 度
UIImage getting rotated 90 degrees after cropping
我有以下功能来裁剪 UIImage 的一部分和 return 裁剪后的 UIImage。但是,在使用这个裁剪后的 UIImage 时,它已经旋转了 90 度。我的图像通常在 "portrait mode" 中,我想你可以这么说,并且我试图在裁剪后保持它的状态。
我知道还有其他帖子有此问题,但我已尝试实施解决方案并且 none 对我有用。
private func cropImage(image: UIImage, cropRect: CGRect) -> UIImage {
UIGraphicsBeginImageContextWithOptions(cropRect.size, false, 0);
let context = UIGraphicsGetCurrentContext();
context?.translateBy(x: 0.0, y: image.size.height);
context?.scaleBy(x: 1.0, y: -1.0);
context?.draw(image.cgImage!, in: CGRect(x:0, y:0, width:image.size.width, height:image.size.height), byTiling: false);
context?.clip(to: [cropRect]);
let croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return croppedImage!;
}
由以下内容创建的原始 UIImage:
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer!)
let dataProvider = CGDataProvider(data: imageData as CFData)
let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent)
let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: self.getImageOrientation(forCamera: self.currentCamera))
你正在做的是一种非常奇怪的裁剪方式。从 UIImage 世界进入 CGImage 世界会让你陷入各种困难(正如你所发现的);您会丢失缩放和方向信息,并且图像最终可能会垂直翻转。正常的方式更像这样:
UIGraphicsBeginImageContextWithOptions(cropRect.size, false, 0)
image.draw(at:CGPoint(x:-cropRect.origin.x, y:-cropRect.origin.y))
let croppedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
另请注意,从 iOS 10 开始,整个 UIGraphicsBeginImageContextWithOptions 舞蹈已经过时,因为您可以改用 UIGraphicsImageRenderer。
我有以下功能来裁剪 UIImage 的一部分和 return 裁剪后的 UIImage。但是,在使用这个裁剪后的 UIImage 时,它已经旋转了 90 度。我的图像通常在 "portrait mode" 中,我想你可以这么说,并且我试图在裁剪后保持它的状态。
我知道还有其他帖子有此问题,但我已尝试实施解决方案并且 none 对我有用。
private func cropImage(image: UIImage, cropRect: CGRect) -> UIImage {
UIGraphicsBeginImageContextWithOptions(cropRect.size, false, 0);
let context = UIGraphicsGetCurrentContext();
context?.translateBy(x: 0.0, y: image.size.height);
context?.scaleBy(x: 1.0, y: -1.0);
context?.draw(image.cgImage!, in: CGRect(x:0, y:0, width:image.size.width, height:image.size.height), byTiling: false);
context?.clip(to: [cropRect]);
let croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return croppedImage!;
}
由以下内容创建的原始 UIImage:
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer!)
let dataProvider = CGDataProvider(data: imageData as CFData)
let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent)
let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: self.getImageOrientation(forCamera: self.currentCamera))
你正在做的是一种非常奇怪的裁剪方式。从 UIImage 世界进入 CGImage 世界会让你陷入各种困难(正如你所发现的);您会丢失缩放和方向信息,并且图像最终可能会垂直翻转。正常的方式更像这样:
UIGraphicsBeginImageContextWithOptions(cropRect.size, false, 0)
image.draw(at:CGPoint(x:-cropRect.origin.x, y:-cropRect.origin.y))
let croppedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
另请注意,从 iOS 10 开始,整个 UIGraphicsBeginImageContextWithOptions 舞蹈已经过时,因为您可以改用 UIGraphicsImageRenderer。