AFNetworking 获取图像,如果它被修改
Posted
技术标签:
【中文标题】AFNetworking 获取图像,如果它被修改【英文标题】:AFNetworking get image if it is modified 【发布时间】:2016-02-10 09:43:09 【问题描述】:如果是新图像,我正在使用 AFNetworking 并下载图像。
在我阅读了***之后,目前我正在这样做。
如果图片没有被修改,http头中会有缓存,我用这个事实来检查图片是否被修改。
它适用于大多数 ios。但是,在 iPhone 6s iOS 9.2.1 上,它总是假定为新图像。
我应该如何检测服务器中的图像是否已经通过使用 AFNetworking 修改或者可能是 NSUrlConnection?
- (void)downloadSplashScreenFromURL:(NSString *)urlStr
BOOL __block responseFromCache = YES; // yes by default
void (^requestSuccessBlock)(AFHTTPRequestOperation *operation, id responseObject) = ^(AFHTTPRequestOperation *operation, id responseObject)
// response was returned from the server, not from cache
NSString *assestName = [urlStr lastPathComponent];
////WRITE TO FILEPATH
NSString *filePath = [splashDirectory() stringByAppendingString:
[NSString stringWithFormat:@"/%@", assestName]];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])
DLog(@"Splash : Splash image is empty");
NSData *pngData = UIImagePNGRepresentation(responseObject);
[pngData writeToFile:filePath atomically:YES];
return ;
if (responseFromCache)
// response was returned from cache
DLog(@"SPLASH - RESPONSE FROM CACHE: %@", responseObject);
else
DLog(@"SPLASH - NEW IMAGES FROM SERVER \n Response: %@", responseObject);
NSData *pngData = UIImagePNGRepresentation(responseObject);
[pngData writeToFile:filePath atomically:YES];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:USERDEFAULTS_SPLASH_SCREEN];
[[SplashHelper sharedInstance] showSplash:YES inWindow:[AppDelegate instance].window andSuccessBlock:^
[[AppDelegate instance] startRunning];
];
;
void (^requestFailureBlock)(AFHTTPRequestOperation *operation, NSError *error) = ^(AFHTTPRequestOperation *operation, NSError *error)
NSInteger statusCode = operation.response.statusCode;
DLog(@"SPLASH - status code: %lu \nERROR: %@", (long)statusCode, [error localizedDescription]);
DLog(@"SPLASH - ERROR: %@", error);
;
DLog(@"Splash : CALL SPLASH SCREEN HELPER");
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestOperation *operation = [manager GET:urlStr
parameters:nil
success:requestSuccessBlock
failure:requestFailureBlock];
[manager.requestSerializer setTimeoutInterval:3.0f];
operation.responseSerializer = [AFImageResponseSerializer serializer];
[operation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse)
// this will be called whenever server returns status code 200, not 304
responseFromCache = NO;
DLog(@"Splash : cachedResponse = %@", cachedResponse);
return cachedResponse;
];
【问题讨论】:
【参考方案1】:我在我的应用程序中使用#import "UIImageView+AFNetworking.h"
类别将图像从我的服务器加载到应用程序。它工作得很好,每当对服务器上的图像进行更新时,它都会生成新的 URL,因此当我使用新的 URL 请求时,AFNetworking
将找不到缓存的图像并从服务器加载新的图像。
您还应该检查这一点,How do I get cached image stored by AFNetworking's UIImageView category? - 当您需要在应用的缓存区域中查看图像时,就会出现要求。
【讨论】:
以上是关于AFNetworking 获取图像,如果它被修改的主要内容,如果未能解决你的问题,请参考以下文章
iOS - 使用 AFNetworking 获取图像保持缓存
获取 System.InvalidOperationException:请求格式无效:通过 AFNetworking 上传图像(数据)时出现多部分/表单数据错误
UIImageView+AFNetworking 未从服务器检索最新图像