2013-05-18 114 views
0

我必须下载图像并将其保存到本地数据库中。所以我将图像存储在NSData中,并将其插入到本地数据库中。但有至少50个图像来自服务器,因此将图像存储到NSData中,然后插入本地数据库需要更多时间。有没有解决方案,以便消耗更少的时间。从服务器下载图像并保存到数据库中

请给我建议。

+0

没有足够的信息。什么是图像?你需要多久才能保持它们? – trojanfoe

回答

0

从互联网处理图像下载的更常见方法是将其缓存到内存(NSURLCache)或磁盘(SDWebImageView)中。并且只保存'图片网址'到数据库。

缓存机制将找到您使用该URL的图片下一张图片。

0

尝试将文件保存在光盘上,并把文件路径为数据的基础上,而不是插入的BLOB到数据库

换句话说: 您的数据库将包含图像元数据和绝对路径的文件

你可以这样做,即使这样:

  • 创建文件路径

  • 虽然DOWNLO ading您的图片 创建的文件路径附加字节的文件(几种方法可以做到 - 手动使用 NSURLConnection的,或使用一些库像AFHTTPConnection与NSStream 附后)

  • 检查,如果一切正常,然后把文件路径字符串到您的 数据库或清理文件和有关错误报告

但最好是:

  • 下载文件到临时目录

  • 检查一切正常,然后移动到永久目录和存储文件路径数据库

  • 清洁你的临时目录,如果需要

  • 清理临时目录regulary(顺便说一下,iOS有时会清除它)

0

只是尝试与HCDownload它是一个组件,您用于从URL下载图像。只需下载这个类,并使用下面的代码,这是很容易的。一旦你的下载完成后,委托方法finishedDownloadingURL被逐一调用所有图像,然后将其路径(存储在你存储的路径)存储在数据库中。

HCDownload

使用下面这是形容。

.h文件中

#import "HCDownloadViewController.h" 
@interface HomeViewController_iPhone : UIViewController<HCDownloadViewControllerDelegate> 
{ 
    HCDownloadViewController *tblDownloadHairStyle; 
} 

@property (nonatomic,retain) HCDownloadViewController *tblDownloadHairStyle; 

。M档

@synthesize tblDownloadHairStyle; 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    tblDownloadHairStyle=[[HCDownloadViewController alloc] init]; 
    tblDownloadHairStyle.delegate=self; 
} 


    //Where you download the image 
     [self createDocumentDirectory:@"MyFolder"]; //create folder for save photo 
    NSString *pathHair=[self getDocumentDirectoryPath:@"MyFolder"]; 
    tblDownloadHairStyle.downloadDirectory = pathHair; 

    // your other code just get the image path 
    NSString *strimage_path=[hairDictonary objectForKey:@"image_path"]; 
    strimage_path=[NSString stringWithFormat:@"http://yoururr.com/%@",strimage_path]; 
    [tblDownloadHairStyle downloadURL:[NSURL URLWithString:strimage_path] userInfo:hairDictonary]; 


#pragma mark- 
#pragma mark-HCDownloadViewController Delegate Method 

- (void)downloadController:(HCDownloadViewController *)vc startedDownloadingURL:(NSURL *)url userInfo:(NSDictionary *)userInfo { 
     NSLog(@"startedDownloadingURL=%@",url); 
    } 

- (void)downloadController:(HCDownloadViewController *)vc finishedDownloadingURL:(NSURL *)url toFile:(NSString *)fileName userInfo:(NSDictionary *)userInfo { 
    NSLog(@"finishedDownloadingURL =%@",url); 
} 

- (void)downloadController:(HCDownloadViewController *)vc failedDownloadingURL:(NSURL *)url withError:(NSError *)error userInfo:(NSDictionary *)userInfo { 
    NSLog(@"failedDownloadingURL=%@",url); 
    } 

#pragma mark - File Functions - Document Functions 
-(void)createDocumentDirectory:(NSString*)pStrDirectoryName 
{ 
    NSString *dataPath = [self getDocumentDirectoryPath:pStrDirectoryName]; 

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
     [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:NULL]; 
} 

-(NSString*)getDocumentDirectoryPath:(NSString*)pStrPathName 
{ 
    NSString *strPath = @""; 
    if(pStrPathName) 
     strPath = [[kAppDirectoryPath objectAtIndex:0] stringByAppendingPathComponent:pStrPathName]; 

    return strPath; 
} 

编辑

另一个简单的办法是

ImageDownload

+0

看到我编辑的答案,其他参考.... –

相关问题