iPhone - UIImagePickerController ->将图像保存到应用程序文件夹
我有一个使用 UIImagePickerController
的 iPhone 应用程序。作为 sourceType
我有
- UIImagePickerControllerSourceTypePhotoLibrary
- UIImagePickerControllerSourceTypeCamera
- UIImagePickerControllerSourceTypeSavedPhotosAlbum
,因此用户可以制作照片或从照片或相机照片中的照片库中选择一张照片。
图像将显示在 UIImageView 中。如果用户关闭应用程序,则应保存图像。因此,对于文本字段,我使用 NSUserDefaults。我知道,用 NSData 将图像保存在 NSUSerDefaults 中并不是一个好方法,所以我想将图像保存/复制到由我的应用程序控制的文件夹中,类似于 NSUserDefaults。
我该怎么做?我想保存它,然后我可以将文件的路径写入我的 NSUserDefaults 并在应用程序启动时读取它。
预先感谢您&此致。
I have an iPhone application using a UIImagePickerController
. As sourceType
I have
- UIImagePickerControllerSourceTypePhotoLibrary
- UIImagePickerControllerSourceTypeCamera
- UIImagePickerControllerSourceTypeSavedPhotosAlbum
so the user can make a photo or select one from the photo library from the photos or camera photos.
The image will be displayed in a UIImageView. The image should be saved if the user closes the app. So for text fields, I use NSUserDefaults
. I know, it is not a good way to save the image inside the NSUSerDefaults with NSData, so I want to save / copy the image to a folder which is controlled by my application similar to the NSUserDefaults.
How can I do this? I want to save it, and then I can write the path to the file into my NSUserDefaults
and read it on startup of the application.
Thank you in advance & Best Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
中使用以下代码
您可以在 XueYu 的
UIImagePickerControllerDelegate
委托实现UPDSwift 3.0 代码
You can use the following code in
UIImagePickerControllerDelegate
delegate implementationUPD
Swift 3.0 code by XueYu
根据您要保存的文件格式,您可以使用
OR
Depending on the file format you want to save, you can use
OR
这是将 UIImage 保存到文档目录中的代码。您可以在 didFinishPickingImage 委托方法中使用此代码:
您还可以使用:
查找应用程序文档目录的路径,而不是 NSHomeDierctory。
This is the code to save the UIImage into the document directory. You can use this code in didFinishPickingImage delegate method:
You can also use:
to find the path to your application document directory, instead of NSHomeDierctory.
更新 knuku 对 Swift 3.0 的答案
,此处图像保存为 jpeg,但您也可以将其保存为 png。 0.0 参数代表压缩,它是最低质量,如果您想获得最佳效果,请使用 1.0。
Update knuku's answer for Swift 3.0
here the image is saved as jpeg but you can also save it as png. the 0.0 parameter stands for compression and it's the lowest quality, if you want to get the best use 1.0.