1

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?

Kara
  • 6,115
  • 16
  • 50
  • 57
Gal Gibli
  • 474
  • 3
  • 6
  • 16

2 Answers2

1

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 UIImageJpegRepresentation or UIImagePNGRepresentation). 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

Community
  • 1
  • 1
Stavash
  • 14,244
  • 5
  • 52
  • 80
  • The link you provided it the end is the one I was talking about.. They say it returns "nil" there, and the other one dosent have any "OK" on it.. is it good? – Gal Gibli Jan 30 '13 at 16:10
  • I think it isn't a robust solution anyway - I'd think of a way to obtain UIImages another way - maybe query facebook via FQL? I wouldn't try to traverse subviews and check if they have a UIImageView, very not-elegant. – Stavash Jan 30 '13 at 16:17
  • I don't know how to use the FQL. Can I just extract the fbprofilepictureview's sub view? (the image) and save it to the app folder? if so, how.. – Gal Gibli Jan 30 '13 at 19:08
  • Again, you should be able to do so (not sure how) but it wouldn't be the best way – Stavash Jan 31 '13 at 07:47
  • For querying friends with FQL, see step 2 in this guide: http://developers.facebook.com/docs/howtos/run-fql-queries-ios-sdk/#step2 – Stavash Jan 31 '13 at 08:21
0

Here is an example to cache With NSUserDefaults

//save

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myImage];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:@"yourKeyImage"];
[defaults synchronize];

//get

NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"yourKeyImage"];
NSData *received = [NSKeyedUnarchiver unarchiveObjectWithData:data];
UIImage *image = [UIImage imageWithData:received];