我正在使用一个名为 DFImageManager
的库,但我不太确定我是否以正确的方式使用它。
我被引导相信在 requestImageForRequest
的完成 block 中,info
可用于检测请求是否成功。
但是,有 10% 的时间 info
为 nil
但 image
具有值。
info
参数有什么用?当info
为nil
时,我还能使用image
的值吗?
DFImageRequestOptions *options = [[DFImageRequestOptions alloc] init];
options.expirationAge = 60;
DFImageRequest *request = [DFImageRequest requestWithResource:[NSURL URLWithString:imageURL] targetSize:imageView.frame.size contentMode:DFImageContentModeAspectFill options:options];
[[DFImageManager sharedManager] requestImageForRequest:request completion:^(UIImage *image, NSDictionary *info) {
if (info != nil) {
[imageView setImage:image];
}
else {
// use a placeholder
}
}];
最佳答案
Completion block is guaranteed to be called on the main thread. Completion block is called synchronously when the requested image can be retrieved from the memory cache and the request was made on the main thread.
当 info
为 nil
时就是这种情况。该实现类似于 PHImageManager
,它有时会根据请求同步(而不是异步)调用完成 block 。这是必要的,这样当图像在内存缓存中实际可用时,您可以重新加载 UI 而不必担心使用空 ImageView 绘制帧。单个 requestImageForRequest
方法可使内存缓存对客户端透明。
What the info param is for?
如果存在一个错误(请参阅 DFImageInfoErrorKey
键)。和一个请求 ID (DFImageInfoRequestIDKey
)。但是当请求由内存缓存处理时,信息为零。
When the info is nil, can I still use the value of image?
是的,你可以。你应该总是检查图像是否不是 nil 而不是检查信息:
if (image != nil) {
[imageView setImage:image];
} else {
// use a placeholder
NSError *error = info[DFImageInfoErrorKey];
// handle a specific error (if there is one)
}
我将在下一个版本中使可空性变得更加明显:
requestID
应该是nonnull
。info
应该是nonnull
(即使请求可以由内存缓存处理)并且应该始终包含一个requestID
。request
及其resource
都应该是nonnull
。- 图像管理器应在请求无效(不支持的 Assets )时引发异常。
关于ios - 我是否在 DFImageManager 中正确使用了 requestImageForRequest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30927760/