-1

I am using Soap API and it will return a response in XML file that xml file I stored in SQLite Database. That xml file consists of image url those images I am downloading and storing them into local folder. But that downloading takes much time to download and store in local folder.Now what I want is logic for speed download and store them in local folder.

Here is my code:

 if (categoryArray.count > 0)
    NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"WordImages"];
    NSError *error = nil;
    if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:stringPath  withIntermediateDirectories:NO attributes:nil error:&error];
    NSString *fileName = [stringPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Image%lu.jpg",(unsigned long)categoryTitleArray.count]];
    NSString *imgUrl = [categoryArray objectAtIndex:0];
    imgUrl = [imgUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL  URLWithString:imgUrl]];
    [data writeToFile:fileName atomically:YES];
 categoryArray = [[NSMutableArray alloc]init];
    [imageArray addObject:fileName];
Madhu
  • 439
  • 2
  • 14
  • Why do you store images locally, you try to implement a caching feature ? – AnthoPak Jun 23 '16 at 08:18
  • Although your question is not very clear, this line of code looks very evil `[NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]]`. Research background operations to fix it. – Desdenova Jun 23 '16 at 08:19
  • In any case use an asynchronous pattern to download data, it makes better user experience and doesn't block the thread. – vadian Jun 23 '16 at 08:19

1 Answers1

2

You can use the sdwebimage library it will download the images and cache it.

Check my Answer here

Community
  • 1
  • 1
Raza.najam
  • 769
  • 6
  • 15
  • Yes. you need to import files of sdwebimage into your project – Raza.najam Jun 23 '16 at 08:40
  • I think you can accept my answer if it resolves your problem. – Raza.najam Jun 23 '16 at 10:37
  • It will be fast, as it cache those images. – Raza.najam Jun 23 '16 at 11:15
  • You need to display the image on screen, so you can save the image url of images. Once those url's are saved you can used them whenever you want to display the image on screen using below code [YOUR IMAGEVIEW setImageWithURL:[NSURL URLWithString:@"IMAGE URL"] placeholderImage:[UIImage imageNamed:@"PLACEHOLDER IMAGE NAME"]]; – Raza.najam Jun 23 '16 at 11:33