Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load images from NSURL async with RestKit

Is there a wrapper or some sort of built-in functionality available in RestKit to load a UIImage from an NSURL asynchronously using callbacks or blocks? I could not find such a method in the RestKit docs. If there is not, what is a good strategy for implementing lazy loaded async images from NSURL using RestKit as much as possible?

like image 252
tacos_tacos_tacos Avatar asked Mar 21 '12 06:03

tacos_tacos_tacos


2 Answers

Not sure about a RestKit solution, but SDWebImage is a library that will let you easily load images asynchronously by adding a category to UIImageView, so all you have to write is this (for example):

[myImageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
like image 172
jonkroll Avatar answered Oct 20 '22 20:10

jonkroll


Using RestKit you can use RKRequest to load the data for the image in a manner such as:

RKRequest* request = [RKRequest requestWithURL: url];

request.onDidLoadResponse = ^(RKResponse* response) {
    UIImage* image = [UIImage imageWithData: response.body];
    // do something interesting with the image
};

request.onDidFailLoadWithError = ^(NSError* error) {
    // handle failure to load image
}

[imageLoadingQueue addRequest: request];

Note that even in the onDidLoadResponse case you may want to check response to make sure the type of data is what you expected. The image loading queue used above can be created like so:

imageLoadingQueue = [RKRequestQueue requestQueueWithName: @"imageLoadingQueue"];
[imageLoadingQueue start];
like image 39
tgaul Avatar answered Oct 20 '22 20:10

tgaul