Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

iphone - CoreData : store images to DB or not?

I am making an app that takes photos from web site for some Username and shows it in a UITable with username then when clicking user name it shows photos for this user and then clicking to name of photo it shows full screen photo.

My question is I am using NSData to get photos from internet. Do I have to save the data to CoreData? When pressing name of user it creates NSData and downloads photos from internet and shows them on UITable. And it takes time.

What is good approach? and How can save this images to CoreData?

I am using this method

NSData *imageData=[flickr dataForPhotoID:firstPhoto.id fromFarm:firstPhoto.farm 
onServer:firstPhoto.server withSecret:firstPhoto.secret inFormat: 
FlickrFetcherPhotoFormatSquare];

and here definition of dataForPhotoID method

- (NSData *)dataForPhotoID:(NSString *)photoID fromFarm:(NSString *)farm   
   onServer:(NSString *)server withSecret:(NSString *)secret 
 inFormat:(FlickrFetcherPhotoFormat)format {

#if TEST_HIGH_NETWORK_LATENCY
sleep(1);
#endif

NSString *formatString;

switch (format) {
    case FlickrFetcherPhotoFormatSquare:    formatString = @"s"; break;
    case FlickrFetcherPhotoFormatLarge:     formatString = @"b"; break;
}

NSString *photoURLString = [NSString stringWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_%@.jpg", farm, server, photoID, secret, formatString];
NSURL *url = [NSURL URLWithString:photoURLString];

return [NSData dataWithContentsOfURL:url];
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, always store your images in a usable format such as PNG or JPEG instead of NSData. This will save you a lot of headaches.

Second, the rule for storing binary data is:

  • < 100kb store in the same table as the relevant data
  • < 1mb store in a separate table attached via a relationship to avoid loading unnecessarily
  • 1mb store on disk and reference it inside of Core Data

Update

The storage inside of Core Data should be binary and you can write accessor methods for it. Take a look at this answer: Core data images from desktop to iphone

Update

The example code I linked to describes how to create accessors in your NSManagedObject subclass that will convert the image back and forth between a UIImage and binary data.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...