尝试将我的图像方向旋转270度我可以旋转90度并且图像出现但是当旋转图像270时没有任何反应。我添加了2张90度和270度命令的图片,以使事情更加清晰。
extension CIImage {
var image: UIImage? { return UIImage(ciImage: self) }
var rotatingLeft: CIImage? {
let transform = CGAffineTransform(translationX: extent.midX, y: extent.midY)
.rotated(by: CGFloat.pi.divided(by: 2))
.translatedBy(x: -extent.midX, y: -extent.midY)
return CIFilter(name: "CIAffineTransform", withInputParameters: [kCIInputImageKey: self, kCIInputTransformKey: NSValue(cgAffineTransform: transform)])?.outputImage
}
var rotatingRight: CIImage? {
let transform = CGAffineTransform(translationX: extent.midX, y: extent.midY)
.rotated(by: -CGFloat.pi.divided(by: 2))
.translatedBy(x: -extent.midX, y: -extent.midY)
return CIFilter(name: "CIAffineTransform", withInputParameters: [kCIInputImageKey: self, kCIInputTransformKey: NSValue(cgAffineTransform: transform)])?.outputImage
}
}
extension UIImage {
var ciimage: CIImage? { return CIImage(image: self) }
}
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var displayImage: UIImageView!
var currentImageView: UIImageView?
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
self.currentImageView?.image = displayImage.image
self.currentImageView?.image = image.ciimage?.rotatingLeft?.image
self.dismiss(animated: true)
}
func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
} else {
let ac = UIAlertController(title: "Image Saved!", message: "Your image has been saved to your photos.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}
@IBAction func takePhoto(_ sender: Any) {
self.currentImageView = self.displayImage
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}}
答案 0 :(得分:2)
您可以扩展CIImage而不是UIImage,并使用转换矩阵使用核心图像过滤器CIAffineTransform。开始将其平移到中心,将其旋转90度(.pi / 2)并将其向后移动:
extension CIImage {
var image: UIImage? { return UIImage(ciImage: self) }
func rotated(_ angle: CGFloat) -> CIImage? {
let transform = CGAffineTransform(translationX: extent.midX, y: extent.midY)
.rotated(by: angle * .pi / 180)
.translatedBy(x: -extent.midX, y: -extent.midY)
return CIFilter(name: "CIAffineTransform", withInputParameters: [kCIInputImageKey: self, kCIInputTransformKey: NSValue(cgAffineTransform: transform)])?.outputImage
}
var rotatedLeft: CIImage? { return rotated(90) }
var rotatedRight: CIImage? { return rotated(-90) }
}
游乐场测试:
let image = UIImage(data: try! Data(contentsOf: URL(string:"https://i.stack.imgur.com/Xs4RX.jpg")!))!
image.ciimage?.rotated(22.5)?.image
image.ciimage?.rotatedRight?.image
image.ciimage?.rotatedLeft?.image