gpt4 book ai didi

ios - 如何更改 imagePicker 中的图像方向(swift3)

转载 作者:行者123 更新时间:2023-11-30 12:30:39 25 4
gpt4 key购买 nike

使用图像选择器功能拍照时如何更改图像方向。我不想旋转 imageView。我只想使用图像方向将拍摄的图像颠倒显示在 imageView 中。

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 = image
self.dismiss(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)

}}

errors from answer

最佳答案

要真正直接回答您的问题,您可以旋转 UIImage 本身或旋转 imageView。在这里,我为您编写了一个函数,您可以在 UIImage 的扩展中实现该函数,该函数会将图像上下翻转。使用起来非常简单,尽管没有更多上下文,我不知道它是否正是您正在寻找的。您也可能会失去一些质量。

public func flipped() -> UIImage {
var transform: CGAffineTransform = CGAffineTransform.identity
transform = transform.translatedBy(x: self.size.width, y: self.size.height)
transform = transform.rotated(by: CGFloat(Double.pi))
guard let cgImage = self.cgImage, let colorSpace = cgImage.colorSpace, let context: CGContext = CGContext(data: nil, width: Int(self.size.width), height: Int(self.size.height), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { return self }
context.concatenate(transform)
context.draw(cgImage, in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
guard let transformed = context.makeImage() else { return self }
return UIImage(cgImage: transformed)
}

将其放入 UIImage 的扩展中,使用它所需要做的就是将 UIImagePicker 委托(delegate)方法分配更改为 self.currentImageView?。图像 = image.flipped().

关于ios - 如何更改 imagePicker 中的图像方向(swift3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43599114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com