I am using Facebook graph to load a user’s image to an iOS app.
The image is then placed inside a UIView in order to present it.
I would like to present that same picture if internet connection is not available, and if internet is available to fetch the image from Facebook.
How do I save the image:
- to NSUserDefaults?
- to Core Data?
- to App folder?
and how do I retrieve it later?
You’re talking about image caching – keep in mind that you can’t just infinitely store images as they are downloaded (there is a chance that your application will take up way too much disk space eventually). You need to define some criteria for purging all of this data – either data that is unused or maybe outdated – that’s up to you. There are a lot of ways to tackle the caching issue, just Google it and I’m sure you’ll find some useful stuff.
As for storing images – what you want to do is get the UIImage’s data (by using functions like
UIImageJpegRepresentationorUIImagePNGRepresentation). These functions will return an NSData instance that can either be stored in NSUserDefaults (not the place for image caching) or on a file in your Cache directory (much better and won’t give Apple a good reason to reject your app for bad storage practices).Also, don’t accidentally try to call these functions on an FBProfilePictureView instance – that’s just a class Facebook created for displaying profile pictures (it is in fact a UIView that has a UIImageView subview of a profile image. Within this UIImageView you can get the UIImage itself). How can I convert FBProfilePictureView to an UIImage?
Good luck