ios - 我是否在 DFImageManager 中正确使用了 requestImageForRequest?

标签 ios objective-c

我正在使用一个名为 DFImageManager 的库,但我不太确定我是否以正确的方式使用它。

我被引导相信在 requestImageForRequest 的完成 block 中,info 可用于检测请求是否成功。

但是,有 10% 的时间 infonilimage 具有值。

info 参数有什么用?当infonil时,我还能使用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.

infonil 时就是这种情况。该实现类似于 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 应该是 nonnullinfo 应该是 nonnull(即使请求可以由内存缓存处理)并且应该始终包含一个 requestID
  • request 及其resource 都应该是nonnull
  • 图像管理器应在请求无效(不支持的 Assets )时引发异常。

关于ios - 我是否在 DFImageManager 中正确使用了 requestImageForRequest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30927760/

相关文章:

iphone - 为什么我的应用程序中着色器的路径为零?

ios - 使用界面生成器将选项卡栏添加到 NavigationController,一点也不直观

iphone - 即使重新加载表格后文本的颜色也不会改变

ios - Titanium : build error after upgrade to 3. 5.0.GA(体系结构 x86_64 的 undefined symbol )

objective-c - NSTextField 和 NSAttributedString 占位符的奇怪行为

iphone - 推送通知证书错误

ios - Swift - 调用中的额外参数 'image'

ios - 无法使用带有 : (Any), 选项的类型参数列表调用 jsonObject:[任何]

ios - 使用适当的方案主机和域将 NSString 验证为 URL

ios - 线程阻塞 Objective C