【发布时间】:2016-06-26 22:23:29
【问题描述】:
在我的应用程序中,我有一个按钮,我可以用它来打开画廊,如下所示:
@IBAction func selectPhoto() {
let photoPicker = UIImagePickerController()
photoPicker.delegate = self
photoPicker.sourceType = .PhotoLibrary
self.presentViewController(photoPicker, animated: true, completion: nil)
}
我让用户选择一张照片并保存:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
photoImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
let fileName:String = "logo.png"
let arrayPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let pngFileName = arrayPaths.stringByAppendingPathComponent(fileName) UIImagePNGRepresentation(photoImageView.image!)!.writeToFile(pngFileName, atomically:true)
imageSave.setObject(fileName, forKey: "pngFileName")
self.dismissViewControllerAnimated(false, completion: nil)
}
然后从视图控制器,每次用户使用以下代码到达场景时都会显示保存的图片。
override func viewDidLoad() {
super.viewDidLoad()
if imageSave.stringForKey("pngFileName") != nil
{
let path = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let fileName = NSUserDefaults.standardUserDefaults()
.stringForKey("pngFileName")
let imagePath = path.stringByAppendingPathComponent(fileName!)
let image = UIImage(contentsOfFile: imagePath )
photoImageView.image = image
}
问题:当图像加载回 UIImageView 时,图片的方向有时会向右旋转 90 度,有时会向左旋转 90 度,有时会在保存时显示。
请让我知道我在这里做错了什么。任何帮助,将不胜感激。谢谢
【问题讨论】:
-
我试图检测我正在保存的图像的方向,但是我无法将这些方向应用回 viewDidLoad() 中的图像。关于如何将检索到的方向值应用于 viewDidLoad() 中的图像的任何线索?谢谢。
标签: ios iphone xcode swift uiimageview