问题描述
有一个包装或者从某种内置可用的功能 RestKit
来加载的UIImage
的 NSURL
使用异步回调或阻止?我找不到在 RestKit
文档这样的方法。如果没有,什么是从 NSURL
实施延迟加载异步图像的使用的 RestKit $ C一个很好的策略$ C>尽可能?
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?
推荐答案
使用RestKit可以使用 RKRequest
加载数据的方式,如图片:
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];
请注意,即使在 onDidLoadResponse
情况下,你可能要检查响应
,以确保数据的类型你所期望的。上面使用的图像加载队列可以像这样创建:
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];
这篇关于从加载图像NSURL与异步RestKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!