Question

I am implementing a UITableView with custom UITableViewCell. There is UIImageView there for displaying some image. But the api I am given , does not contain the download links of the images that I'll have to show in each UITableViewCell. For getting the images , I'll need to call another GET request that returns byte array stream of the image to be shown on the corresponding Cell.

I know , I can make an Image from NSData coming from the api. But I am not sure how much efficient idea that would be , I am worrying about memory and performance issue. And I want to do asynchronous loading in UITableViewCell and image cacheing also so that It take less time in later calls. So , what can be a efficient approach here ? And also some suggestion about any good library that can help me doing this would be great.

Was it helpful?

Solution 2

This is how I have solved it. I haven't used caching yet , but this is enough to show them properly in UITableViewCell. Here's the method I have used.

+ (void)downloadImageWithURL:(NSMutableURLRequest *)request completionBlock:(void (^)(BOOL succeeded, UIImage *image))completionBlock
{
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                               NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
                               int statusCode = httpResponse.statusCode ;
                               NSLog(@"Status Code Imageloading : %d" , statusCode);

                               if(statusCode == 200){
                                   UIImage *image = [[UIImage alloc] initWithData:data];
                                   NSLog(@"Success");
                                   completionBlock(YES,image);
                               } else {
                                   NSLog(@"Failure");
                                   completionBlock(NO,nil);
                               }
    }];
}

OTHER TIPS

You will need to download the images on your own, likely in your table view controller or a model. To save on memory space, you can resize the images. Ideally, this will be done on the server side to save on bandwidth. You could, however, resize it once it is downloaded as well. Remember, retina and non-retina devices will have different image resizing needs.

Either way, cache the resized image along with your other cell data and throw away the NSData. You can then refresh the cell by calling reloadRowsAtIndexPaths:withRowAnimation: on your UITableview.

If you have a large number of rows with images, you can write the images to disk and free up memory. In general though, it is best to avoid hitting the disk to load cell data in order to keep scrolling through the table smooth. To keep performance up, you could, based on the table scrolling position, anticipate what images are needed and intelligently load dump images. You would need to do the loading on a separate thread to keep from affecting scrolling performance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top