如何获取AFNetworking的UIImageView类别存储的缓存图像? [英] How do I get cached image stored by AFNetworking's UIImageView category?
问题描述
我在我的应用中使用了 AFNetworking
中的以下类别 UIImageView + AFNetworking.h
有一次,我想获取一张已经在缓存中的图像。
因此,我深入研究上述类别,在其中找到了我认为可以帮助您的以下代码。
以下是其中的代码段:
@implementation AFImageCache
-(UIImage *)cachedImageForRequest:(NSURLRequest *)request {
开关([request cachePolicy]){
case NSURLRequestReloadIgnoringCacheData:
case NSURLRequestReloadIgnoringLocalAndRemoteCacheData:
return nil;
默认值:
休息时间;
}
return [self objectForKey:AFImageCacheKeyFromURLRequest(request)];
}
-(void)cacheImage:(UIImage *)image
forRequest:(NSURLRequest *)request
{
if(image&& ; request){
[self setObject:image forKey:AFImageCacheKeyFromURLRequest(request)];
}
}
@end
如果您要我添加此内容的网络版本,它已经
我不确定如何解决这些警告并从缓存中获取图像?还是其他更合适的方法?
我假设您导入了UIImageView + AFNetworking.h标头。
如果要访问缓存的图像。您应该有一个NSURLRequest对象。然后是这样的:
UIImage * image = [[UIImageView sharedImageCache] cachedImageForRequest:request];
完成!
I'm using following category UIImageView+AFNetworking.h
from AFNetworking
in my app, its working fine, its caching photos for me and loads images smoothly.
At one point, I want to get an image which is already there in my cache.
So I dig up into above category class where I found following code, which I think – can be helpful.
Here's the snippet from it:
@implementation AFImageCache
- (UIImage *)cachedImageForRequest:(NSURLRequest *)request {
switch ([request cachePolicy]) {
case NSURLRequestReloadIgnoringCacheData:
case NSURLRequestReloadIgnoringLocalAndRemoteCacheData:
return nil;
default:
break;
}
return [self objectForKey:AFImageCacheKeyFromURLRequest(request)];
}
- (void)cacheImage:(UIImage *)image
forRequest:(NSURLRequest *)request
{
if (image && request) {
[self setObject:image forKey:AFImageCacheKeyFromURLRequest(request)];
}
}
@end
If you want me to add web version of this, it's already here.
I think, - (UIImage *)cachedImageForRequest:(NSURLRequest *)request
is the method which can return me my cached image.
But I'm not sure, how can I use it?
If I know Objective-C a bit, it's a kinda a "protocol"?
What I have tried so far is to use it like a protocol in one of my view controller. Like this:
#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController<AFImageCache>
@end
But then it is giving me following warnings:
I'm not sure how to resolve those warnings and get the image from cache? Or any other more appropriate way?
I assume you import UIImageView+AFNetworking.h header.
If you want to access to the cached image. You should have a NSURLRequest object. Then it is like this:
UIImage *image = [[UIImageView sharedImageCache] cachedImageForRequest:request];
Done!
这篇关于如何获取AFNetworking的UIImageView类别存储的缓存图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!