Is it possible to save images into NSUserDefaults
as an object and then retrieve for further use?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Could I create “Call” button in HTML 5 IPhone appl
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
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
.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.Yes , technically possible as in
[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(image) forKey:@"foo"];
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.
For Swift 3 and JPG format
Register Default Image :
Save Image :
Load Image :
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.
To save an image in NSUserDefaults:
To retrieve an image from NSUserDefaults: