从服务器下载图像并保存到数据库中 [英] Downloading images from server and saving into Database

查看:88
本文介绍了从服务器下载图像并保存到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须下载图像并将其保存到我的本地数据库中.所以我将图像存储在 NSData 中,而不是将其插入到本地数据库中.但是至少有 50 张图像来自服务器,因此将图像存储到 NSData 中,然后插入到本地数据库中需要更多时间.有什么解决方案可以减少它消耗的时间.

I have to download images and save it into my local database. So I am storing the images in NSData and than inserting it into local database. But there are atleast 50 images coming from the server so storing the images into NSData and then inserting into local database it taking more time. Is there any solution so that it will consume less time.

请推荐我.

推荐答案

试试 HCDownload 它是一个用于从 url 下载图像的组件.只需下载这个类并使用下面的代码就很容易了.下载完成后,会为所有图像一一调用委托方法finishedDownloadingURL,然后将其路径(您存储图像的路径)存储在数据库中.

just try with HCDownload it is a component that you use for downloading image from url. just download this class and use below code it is very easy. once your download is complete then delegate method finishedDownloadingURL is called for all images one by one then store its path (path of image where you stored) on database.

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;
}

编辑

另一种简单的方法是

图片下载

这篇关于从服务器下载图像并保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆