从 UIImagepickercontroller 选择后如何保存图像
How to save image after selecting from UIImagepickercontroller
我正在使用 Swift 和 Xcode 构建一个 iOS 应用程序 6. 我已经实现了从 UIImagePickerController 选择图像并设置到 UIImageView 的功能。
这是我的代码 select 来自 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 中显示。
有人能帮我实现这个吗?
感谢帮助!
提前致谢!
试试这个。
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)
我正在使用 Swift 和 Xcode 构建一个 iOS 应用程序 6. 我已经实现了从 UIImagePickerController 选择图像并设置到 UIImageView 的功能。
这是我的代码 select 来自 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 中显示。
有人能帮我实现这个吗?
感谢帮助!
提前致谢!
试试这个。
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)