将图像保存在 NSUserDefaults 中?
是否可以将图像作为对象保存到 NSUserDefaults 中,然后检索以供进一步使用?
Is it possible to save images into NSUserDefaults
as an object and then retrieve for further use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
要将图像保存在 NSUserDefaults 中:
要从 NSUserDefaults 检索图像:
To save an image in NSUserDefaults:
To retrieve an image from NSUserDefaults:
注意!如果您在 iOS8/XCODE6 下工作,请参阅下面的更新
对于那些仍在寻找答案的人,这里是在 NSUserDefaults 中保存图像的“建议”方法的代码。你不应该将图像数据直接保存到 NSUserDefaults 中!
写入数据:
读取数据:
documentsPathForFileName:
适用于 iOS8/XCODE6
正如 tmr 和 DevC 在下面的评论中提到的,xcode6/ios8 存在问题。 xcode5 和 xcode 6 安装过程之间的区别在于,xcode6 每次在 xcode 中运行后都会更改应用程序 UUID(请参阅路径中突出显示的部分:/var/mobile/Containers/Data/Application/B0D49CF5- 8FBE-4F14-87AE-FA8C16A678B1/Documents/image.jpg)。
因此有 2 个解决方法:
这里是 swift代码版本作为奖励(使用第二种方法):
写入数据:
读取数据:
documentsPathForFileName:
ATTENTION! IF YOU'RE WORKING UNDER iOS8/XCODE6 SEE MY UPDATE BELOW
For those who still looking for answer here is code of "advisable" way to save image in NSUserDefaults. You SHOULD NOT save image data directly into NSUserDefaults!
Write data:
Read data:
documentsPathForFileName:
For iOS8/XCODE6
As tmr and DevC mentioned in comments below there is a problem with xcode6/ios8. The difference between xcode5 and xcode 6 installation process is that xcode6 changes apps UUID after each run in xcode (see hightlighted part in path: /var/mobile/Containers/Data/Application/B0D49CF5-8FBE-4F14-87AE-FA8C16A678B1/Documents/image.jpg).
So there are 2 workarounds:
Here is swift version of code as a bonus (with 2nd approach):
Write data:
Read data:
documentsPathForFileName:
虽然可以将
UIImage
保存到NSUserDefaults
,但通常不推荐这样做,因为这不是保存图像的最有效方法;更有效的方法是将图像保存在应用程序的文档目录中。出于此问题的目的,我已附上您问题的答案,以及保存
UIImage
的更有效方法。NSUserDefaults(不推荐)
保存到 NSUserDefaults
此方法允许您将任何
UIImage
保存到NSUserDefaults
。您可以这样称呼它:
从 NSUserDefaults 加载
此方法允许您从
NSUserDefaults
加载任何UIImage
。您可以这样称呼它:
更好的替代方法
保存到文档目录
此方法允许您将任何
UIImage
保存到应用内的文档目录
。您可以这样称呼它:
从文档目录加载
此方法允许您从应用程序的
文档目录
加载任何UIImage
。这就是您对它的称呼:
不同的替代方法
将 UIImage 保存到照片库
此方法允许您将任何
UIImage
保存到设备的照片库
,调用方式如下:将多个 UIImage 保存到照片库
此方法允许您将多个
UIImage
保存到设备的照片库
。这就是你如何称呼它:
其中
images
是由UIImages
组成的NSArray
。While it is possible to save a
UIImage
toNSUserDefaults
, it is often not recommended as it is not the most efficient way to save images; a more efficient way is to save your image in the application'sDocuments Directory
.For the purpose of this question, I have attached the answer to your question, along with the more efficient way of saving a
UIImage
.NSUserDefaults (Not Recommended)
Saving to NSUserDefaults
This method allows you to save any
UIImage
toNSUserDefaults
.This is how you call it:
Loading From NSUserDefaults
This method allows you to load any
UIImage
fromNSUserDefaults
.This is how you call it:
A Better Alternative
Saving to Documents Directory
This method allows you to save any
UIImage
to theDocuments Directory
within the app.This is how you call it:
Loading From Documents Directory
This method allows you to load any
UIImage
from the application'sDocuments Directory
.This is how you call it:
A Different Alternative
Saving UIImage to Photo Library
This method allows you to save any
UIImage
to the device'sPhoto Library
, and is called as follows:Saving multiple UIImages to Photo Library
This method allows you to save multiple
UIImages
to the device'sPhoto Library
.This is how you call it:
Where
images
is yourNSArray
composed ofUIImages
.对于 Swift 4
我几乎尝试了这个问题中的所有内容,但没有人为我工作。我找到了解决方案。
首先,我为 UserDefaults 创建了一个扩展,如下所示,然后只调用 get 和 set 方法。
为了在 settingsVC 中将图像设置为背景,我使用了下面的代码。
在主VC中:
For Swift 4
I almost tried everything in this question but no one is worked for me. and I found my solution.
first I created an extension for UserDefaults like below, then just called get and set methods.
to set image as background in settingsVC I used code below.
in mainVC :
对于 swift 2.2
存储:
检索:
For swift 2.2
To store:
To retrieve:
是的,技术上可行
,但不建议,因为 plist 不适合存放大块二进制数据,尤其是用户首选项。最好将图像保存到用户文档文件夹并将对该对象的引用存储为 URL 或路径。
Yes , technically possible as in
But not advisable because plists are not appropriate places for large blobs of binary data especially User Prefs. It would be better to save image to user docs folder and store the reference to that object as a URL or path.
对于Swift 3和JPG格式
注册默认图像:
保存图像:
加载图像:
For Swift 3 and JPG format
Register Default Image :
Save Image :
Load Image :
这在技术上是可行的,但不建议这样做。将图像保存到磁盘。 NSUserDefaults 适用于小型设置,而不是大型二进制数据文件。
It's technically possible, but it's not advisable. Save the image to disk instead. NSUserDefaults is meant for small settings, not big binary data files.
从苹果文档来看,
您可以像这样保存图像:-
并像这样阅读:-
From apple documentation,
You can save image like this:-
And read like this:-
将图像保存到 NSUserDefault:
从 NSUserDefault 加载图像:
Save image to NSUserDefault:
Load Image from NSUserDefault:
是的,你可以使用。但由于它是用于存储首选项,因此您可以更好地将图像保存到文档文件夹中。
如果需要,您可以在
NSUserDefaults
中获取该路径。Yes, you can use. But since it is for storage of preferences, you can better save images to document folder.
And you can have the path in the
NSUserDefaults
, if required.由于这个问题具有很高的谷歌搜索索引 - 这是@NikitaTook 在当今时代的答案,即 Swift 3 和 4(带有异常处理)。
注意:此类仅用于读取 JPG 格式的图像并将其写入文件系统。
Userdefaults
的东西应该在它之外处理。writeFile
接受 jpg 图像的文件名(扩展名为 .jpg)和UIImage
本身,如果能够保存则返回 true,否则返回 false无法写入图像,此时您可以将图像存储在Userdefaults
中,这将是您的备份计划,或者只需再试一次。 readFile 函数接收图像文件名并返回 UIImage,如果找到传递给该函数的图像名称,则返回该图像,否则仅返回默认占位符来自应用程序资产文件夹的图像(这样您可以避免令人讨厌的崩溃或其他奇怪的行为)。Since this question has a high google search index - here's @NikitaTook's answer in today's day and age i.e. Swift 3 and 4 (with exception handling).
Note: This class is solely written to read and write images of JPG format to the filesystem. The
Userdefaults
stuff should be handled outside of it.writeFile
takes in the file name of your jpg image (with .jpg extension) and theUIImage
itself and returns true if it is able to save or else returns false if it is unable to write the image, at which point you can store the image inUserdefaults
which would be your backup plan or simply retry one more time. ThereadFile
function takes in the image file name and returns aUIImage
, if the image name passed to this function is found then it returns that image else it just returns a default placeholder image from the app's asset folder (this way you can avoid nasty crashes or other weird behaviors).斯威夫特 4.x
Xcode 11.x
Swift 4.x
Xcode 11.x