I was assigned to before launching the application displaying promotional images. Images are obtained through JSON, which also has a preview time setting.
I am using the following code to store the image from a URL in the / Library / Caches / Images / directory:
// /Library/Caches
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSLocalDomainMask, YES);
// /Library/Caches/Images
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Images"];
NSString *extension = [imageURLString pathExtension];
// /Library/Caches/Images/image_0.jpg
NSString *filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"image_%d.%@", index, extension]];
NSData *file = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURLString]];
[file writeToFile:filePath atomically:YES];
[self.imagesCache addObject:filePath];
Then to display the images I'm using the code:
// Dentro de um loop. current é o índice do loop
UIImage *image = [UIImage imageWithContentsOfFile:[self.imagesCache objectAtIndex:current]];
self.currentImage = [[UIImageView alloc] initWithImage:image];
When I take the image directly from the URL it works but not the cache.
self.currentImage = [[UIImageView alloc]
initWithImage:[UIImage
imageWithData:[NSData
dataWithContentsOfURL:[NSURL URLWithString:imageURLString]]]];
Who could give me a glimpse into this process of image caching? It does not give an error but the images are all black when obtained from the cache.