尝试将我的图像方向旋转 270 度我可以旋转 90 度并且图像出现但是当将图像旋转 270 时没有任何反应.我添加了 2 张 90 度和 270 度命令的图片,以使事情更清楚.
Trying to rotate my image orientation by 270 degrees I can rotate by 90 degrees and the image appears however when rotating the image by 270 nothing happens. I have added 2 pictures of the 90 and 270 degrees commands to make things more clear.
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)
}}
您可以扩展 CIImage 而不是 UIImage,并使用转换矩阵使用核心图像过滤器 CIAffineTransform.开始将其平移到中心,将其旋转 90 度 (.pi/2) 并将其移回:
You can extend CIImage instead of UIImage and use core image filter CIAffineTransform using the transformation matrix. Start translating it to the center, rotate it 90 degrees (.pi/2) and move it back:
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) }
}
<小时>
游乐场测试:
Playground testing:
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
这篇关于将图像旋转 3*Double.pi/2 使图像不出现(swift3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!