-1

Which is the best way to store the downloaded images? From there I should be able to use them anywhere in my application, and images should not be deleted at any case (like low space). Any help please.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user2361155
  • 83
  • 1
  • 9

2 Answers2

0

As per the standard, App related files(Data) need to be stored in document directory only. Once image get download store that images in document directory and maintain unique name for image identification.

-(NSString *)writeDataAsFile:(NSData *)imageData
{

    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentsDirectory = [paths objectAtIndex:0];
    NSString * thumbNailFilename = [NSString stringWithFormat:@"%@.png",[self GetUUID]]; // Create unique iD
    NSString * thumbNailAppFile = [documentsDirectory stringByAppendingPathComponent:thumbNailFilename];

    if ([imageData writeToFile:thumbNailAppFile atomically:YES])
    {
        return thumbNailFilename;
    }

    return nil;
}

use this method to store the image(downloaded NSData) in document directory.

Retrieve image from the document directory like this

UIImage *thumbnailHomeImage = [UIImage imageWithContentsOfFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"%@",imageName]];
Ganapathy
  • 4,594
  • 5
  • 23
  • 41
  • thanks for your comment,i read some thing like some app are got rejected by storing data in documentsdirectory,because some icloud issue is it so... – user2361155 Jul 03 '13 at 09:42
  • I don't think so. May be that app have some other issue. App store will never reject due to this reason. – Ganapathy Jul 03 '13 at 09:46
  • I would recommend to store images in _cache_ instead – or at least mentioning that option as well, especially if the app's intention is not to eat the device's free space. – holex Sep 08 '17 at 11:05
-1

take a look at this image caching library. ive used it quite a few times, its really useful

Fonix
  • 11,447
  • 3
  • 45
  • 74