views:

287

answers:

1

Hey.

I have a tableView in my app that I load in images to from the NSDocumentDirectory. It works, but when scrolling up and down, the app seems to freeze a bit. I know that this is because of the images getting loaded in, and not getting cached, but I don't know how I can load them in later, after they've been created.

The images are .png-s, 10kB to 50kB big and 300*127 in size.

I am currently loading them in with this code:

imagesPath = [NSString stringWithFormat:@"%@/images/", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
if ([fileManager fileExistsAtPath:[imagesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%d.png", rowID]]]) {
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:[imagesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%d.png", rowID]]];
    // If image contains anything, set cellImage to image. If image is empty, use default, noimage.png.
    if (image != nil){
        // If image != nil, set cellImage to that image
        cell.cellImage.image = image;
    }

    [image release];
    image = nil;
}
+1  A: 

Take a look at the SDWebImage repository. It provides everything to perform asynchronous image loading.

Update

I just noticed that there is some typos in the README, so downloading a local file may not work as expected.

Here is some sample code. The view controller has an UIImageView outlet and wants to load the image.jpg file. It implements the SDWebImageManagerDelegate protocol:

- (IBAction)loadImage:(id)sender {
    SDWebImageManager *manager = [SDWebImageManager sharedManager];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *destPath = [documentsDirectory stringByAppendingPathComponent:@"image.jpg"];
    NSURL *url = [NSURL fileURLWithPath:destPath];
    UIImage *cachedImage = [manager imageWithURL:url];
    if (cachedImage)
    {
        imageView.image = cachedImage;
    }
    else
    {
        // Start an async download
        [manager downloadWithURL:url delegate:self];
    }    
}

- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image
{
    imageView.image = image;
}
Laurent Etiemble
I'm not trying to download an image, I'm trying to load in an image from NSDocumentDirectory.
Emil
Downloading should be understood as accessing any resource supported by NSURL. NSURL can be used for several protocols: http, ftp or file. Convert you path to a NSURL and "download" your local file.
Laurent Etiemble
I'm trying to, but it doesn't work! The NSURL is loaded from a string: `file://var/mobile/Applications/RANDOMIDENTIFIER/Documents/images/10.png`.
Emil
I have added some sample code that works.
Laurent Etiemble
Thanks, that will probably work, but I have decided to use the SDWebImage-framework, so that I don't need to take care of downloading and storing images :)Thanks, though!
Emil