imagePickerController图像方向似乎不正确

时间:2016-05-27 11:56:55

标签: ios iphone swift

所以我已经实现imagePickerController didFinishPickingMediaWithInfo实施,我从相机拍摄图像并将其存储为本地文件:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    print(info)

    if let image = info["UIImagePickerControllerOriginalImage"] {
        if let data = UIImagePNGRepresentation(image as! UIImage) {
            let filename = getDocumentsDirectory().stringByAppendingPathComponent("CurrentProfilePicture.png")
            data.writeToFile(filename, atomically: true)
        }
    }
    self.imageController.dismissViewControllerAnimated(true, completion: nil)
}

然后我使用ViewDidAppear中的以下代码向用户显示图片:

override func viewDidAppear(animated: Bool) {
    let filename = getDocumentsDirectory().stringByAppendingPathComponent("CurrentProfilePicture.png")
        let fileManager = NSFileManager.defaultManager()
        if fileManager.fileExistsAtPath(filename) {
            let Profileimage1 = UIImage(contentsOfFile: filename)
            let ciImage = CIImage(image: Profileimage1!)
            let orientation1: UIImageOrientation = (Profileimage1?.imageOrientation)!
            profileImage.image = UIImage(CIImage: ciImage!, scale: 1.0, orientation:orientation1)
        }

}

如果我只是将UIImage直接加载到UIImageView,我会得到错误的方向。我尝试了上述方法,但仍然得到了错误的定位。照片是用相机拍摄的,手机正确向上拍摄,逆时针旋转90度。如果我在didFinishPickingMediaWithInfo方法中检查从信息字典返回的数据,它会指出方向3,尽管我不确定这与它有什么关系。当图像加载到ViewDidAppear时,返回的方向为" Up"但这显然是不正确的。如果我转动手机横向,则方向再次出错。

我做错了什么?谢谢!

0 个答案:

没有答案