我正在使用Swift和Xcode 6构建一个iOS应用程序。我已经实现了从UIImagePickerController中选择图像并设置为UIImageView的功能。
这是来自UIImagePickerController的代码选择图片:
@IBAction func loadImageButtonTapped(sender: UIButton) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
// imagePicker.sourceType = .Camera
//imagePicker.sourceType = .SavedPhotosAlbum
presentViewController(imagePicker, animated: true, completion: nil)
}
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
// imageView.contentMode = .ScaleAspectFit
imageView.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
现在我找到了存储此图像的最佳方式,当用户再次进入应用程序时,我可以获取图像并在uiimageview中显示。
有些人可以帮我解决这个问题。
非常感谢帮助!
提前致谢!
答案 0 :(得分:1)
试试这个。
let imageData: NSData = UIImageJPEGRepresentation(yourImage, 0.9)
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let destinationPath = documentsPath.stringByAppendingPathComponent("filename.jpg")
imageData.writeToFile(destinationPath, atomically: false)
然后再检索图像:
let loadedImage = UIImage(contentsOfFile: destinationPath)