将图像旋转 3*Double.pi/2 使图像不出现(swift3)

Rotating image by 3*Double.pi/2 makes image not appear (swift3)(将图像旋转 3*Double.pi/2 使图像不出现(swift3))
本文介绍了将图像旋转 3*Double.pi/2 使图像不出现(swift3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将我的图像方向旋转 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)