Load images from NSURL async with RestKit(使用 RestKit 从 NSURL 异步加载图像)
问题描述
RestKit 中是否有包装器或某种内置功能可以使用回调或异步方式从 NSURL 加载 UIImage块?我在 RestKit 文档中找不到这样的方法.如果没有,从 NSURL 使用 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?
推荐答案
使用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 情况下,您也可能需要检查 response 以确保数据类型符合您的预期.上面使用的图片加载队列可以这样创建:
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];
这篇关于使用 RestKit 从 NSURL 异步加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 RestKit 从 NSURL 异步加载图像
基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
