CoreData:将图像存储到DB? [英] CoreData : store images to DB or not?
问题描述
我正在做一个应用程序,从网站的照片的一些用户名,并显示在一个UITable用户名,然后当点击用户名称它显示照片的用户,然后点击照片的名称,它显示全屏照片。 / p>
我的问题是我使用NSData从互联网获取照片。我必须将数据保存到CoreData吗?当按用户名称时,它创建NSData并从互联网下载照片,并在UITable上显示它们。需要时间。
什么是好方法?如何将此图片保存到CoreData?
我使用此方法
NSData * imageData = [flickr dataForPhotoID:firstPhoto.id fromFarm:firstPhoto.farm
onServer:firstPhoto.server withSecret:firstPhoto.secret inFormat:
FlickrFetcherPhotoFormatSquare];
此处定义dataForPhotoID方法
<$ p (NSString *)服务器withSecret:(NSString *)密码
(NSString *)server
$($)$ p>
- (NSData *)dataForPhotoID: inFormat:(FlickrFetcherPhotoFormat)格式{
#if TEST_HIGH_NETWORK_LATENCY
sleep(1);
#endif
NSString * formatString;
switch(format){
case FlickrFetcherPhotoFormatSquare:formatString = @s;打破;
case FlickrFetcherPhotoFormatLarge:formatString = @b;打破;
}
NSString * photoURLString = [NSString stringWithFormat:@http://farm%@.static.flickr.com/%@/%@_%@_%@.jpg ,farm,server,photoID,secret,formatString];
NSURL * url = [NSURL URLWithString:photoURLString];
return [NSData dataWithContentsOfURL:url];
}
以诸如PNG或JPEG而不是NSData的可用格式。
其次,存储二进制数据的规则是:
- < 100kb与相关数据存储在同一个表中
- < 1mb存储在通过关系附加的单独的表中,以避免不必要的加载
-
1mb存储在磁盘上并引用Core Data
更新
的核心数据应该是二进制的,你可以写它的访问器方法。看看这个答案:从桌面到iPhone的核心数据映像
更新
我链接的示例代码描述了如何在NSManagedObject子类中创建访问器它将在UIImage和二进制数据之间来回转换图像。
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];
}
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.
这篇关于CoreData:将图像存储到DB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!