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.
Please suggest me.
The more common way to handle image download from the internet is just cache it to the memory(NSURLCache) or disk(SDWebImageView). and only save the 'image url' to the database.
The Cache mechanism will find the image next image you use that URL.
Try to save the files on the disc, and put file paths into data base instead of inserting BLOBs into database
In another words:
Your database will contain images metadata and absolute paths to files
You can do even this:
Create file path
While downloading your image append bytes to the file at this
created file path ( several ways to do - manually by using
NSURLConnection, or use some libs like AFHTTPConnection with NSStream
attached)
Check if everything is OK, then put the file path string into your
database or clean the file and report about error
But it is better to:
Download file to the temporary directory
Check if everything is ok, then move to the permanent directory and store file path in data base
Clean your temporary directory if needed
Clean your temporary directory regulary (By the way, iOS will clean it sometimes)
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
use this as describe below.
.h file
#import "HCDownloadViewController.h"
@interface HomeViewController_iPhone : UIViewController<HCDownloadViewControllerDelegate>
{
HCDownloadViewController *tblDownloadHairStyle;
}
@property (nonatomic,retain) HCDownloadViewController *tblDownloadHairStyle;
.m file
@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;
}
Edit
another simple way is
ImageDownload