使用RestKit从NSURL异步加载图像



RestKit中是否有包装器或某种内置功能可用于使用回调或块从NSURL异步加载UIImage?我在RestKit文档中找不到这样的方法。如果没有,那么使用尽可能多的RestKitNSURL实现延迟加载异步映像的好策略是什么?

不确定RestKit解决方案,但SDWebImage是一个库,可以通过向UIImageView添加类别来轻松异步加载图像,因此您只需编写以下内容(例如):

[myImageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

使用RestKit,您可以使用RKRequest以以下方式加载图像的数据:

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];

请注意,即使在onDidLoadResponse的情况下,您也可能需要检查response,以确保数据类型符合您的预期。上面使用的图像加载队列可以这样创建:

imageLoadingQueue = [RKRequestQueue requestQueueWithName: @"imageLoadingQueue"];
[imageLoadingQueue start];

最新更新