Sqlite 从服务器下载图像并保存到数据库

Sqlite 从服务器下载图像并保存到数据库,sqlite,uiimage,nsdata,Sqlite,Uiimage,Nsdata,我必须下载图像并将其保存到本地数据库中。因此,我将图像存储在NSData中,然后将其插入本地数据库。但至少有50个图像来自服务器,因此将图像存储到NSData中,然后插入本地数据库需要更多时间。是否有任何解决方案可以减少时间消耗 请建议我。处理从internet下载图像的更常见方法是将其缓存到内存(NSURLCache)或磁盘(SDWebImageView)。并且只将“图像url”保存到数据库中 缓存机制将在您使用该URL的下一个图像中找到该图像 尝试将文件保存在光盘上,并将文件路径放入数据库中

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


请建议我。

处理从internet下载图像的更常见方法是将其缓存到内存(NSURLCache)或磁盘(SDWebImageView)。并且只将“图像url”保存到数据库中


缓存机制将在您使用该URL的下一个图像中找到该图像

尝试将文件保存在光盘上,并将文件路径放入数据库中,而不是将blob插入数据库中

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

您甚至可以这样做:

  • 创建文件路径

  • 下载图像时,请在此时将字节附加到文件 创建文件路径(几种方法-使用 NSURLConnection,或使用一些LIB,如与NSStream的AFHTTPConnection 附件)

  • 检查是否一切正常,然后将文件路径字符串放入 数据库或清理文件并报告错误

但最好是:

  • 将文件下载到临时目录

  • 检查是否一切正常,然后移动到永久目录并将文件路径存储在数据库中

  • 如果需要,请清理临时目录

  • 定期清理临时目录(顺便说一句,iOS有时会清理)


试试HCDownload,它是一个用于从url下载图像的组件。只需下载这个类并使用下面的代码,这是非常简单的。下载完成后,将逐个为所有图像调用委托方法finishedDownloadingURL,然后将其路径(存储图像的路径)存储在数据库中

如下文所述使用此选项

.h文件

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

@property (nonatomic,retain) HCDownloadViewController *tblDownloadHairStyle;
编辑

另一个简单的方法是


信息不足。图像是什么?你需要让他们呆多久?
@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;
}