代码之家  ›  专栏  ›  技术社区  ›  Chewie The Chorkie

导入照片时如何保证图像方向正确?

  •  0
  • Chewie The Chorkie  · 技术社区  · 7 年前

    我有一个Swift代码,它使用UIImagePickerController让用户选择照片,应用CIFilter,然后将UIImage应用于UIImageView。问题是,我不知道如何使图像处于UIImagePickerController或Photos应用程序中显示的正确方向。它独立于UIImage的图像方向。例如,两张导入的imageOrientation为“0”且都是纵向的不同照片可以解析为具有两个完全不同方向的新UIImage。也许这是由EXIF信息决定的?也许两者都有?这就是为什么它令人困惑。

    最后,我只想导入UIImagePickerController或照片应用程序中显示的方向。如果他们能做到,我也有办法。

    @objc func imagePickerController(_ picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
    
        let selectedImage : UIImage = image
    
        print("selectedImage.imageOrientation = \(selectedImage.imageOrientation.rawValue)")
    
        let isPortrait:Bool = image.size.height > image.size.width ? true : false
    
        dismiss(animated: true, completion: { () in
            DispatchQueue.main.async {
                self.enableButtons(enabled:false)
                self.activityTextStatus?.setText("Preparing", center:self.view.center, addToView:self.loadingContainer, setRotation:true)
            }
    
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
                self.exportImagesToPreview(capturedImage: selectedImage,
                                           orientation:nil,
                                           isPortrait: isPortrait)
                 // here I apply an effect to the image,
                 // and it may be in the incorrect orientation
    
            }
        })
    
    }
    
    0 回复  |  直到 7 年前
    推荐文章