Search code examples
iphoneobjective-cios4

Download multiple images


i have to display 11 images in single cell ,all that images are downloaded from server, so when my tableview show than i wanna download all images from server & display it in custom cell. I have used lazyloading sample code. But t can download 1 image at a time. it not feasible according to my requirement. Is there any way to solve this problem???


Solution

    1. Create a thread for every image you download from the server using -
    [NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:@"Image URL"];
    
    1. Send a request to the server to retrieve the image using a thread block.
    -(void)downloadImage:(NSString *)inURL {
            NSAutoreleasePool *thePool = [[NSAutoreleasePool alloc]init];
            UIImage *theImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:inURl]];
            [self performSelectorOnMainThread:@selector(uploadImage:) withObject:theImage waitUntilDone:NO];
            [thePool drain];
        }
    
    1. Update the image so you may view it. A view can only be changed by the main thread.
    -(void)uploadImage:(UIImage *)inImage {
        [inImage retain]; // add image to View.[inImage relese];
    }