【发布时间】:2016-02-10 09:43:09
【问题描述】:
如果是新图像,我正在使用 AFNetworking 并下载图像。
在我阅读了stackoverflow之后,目前我正在这样做。
如果图片没有被修改,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;
}];
}
【问题讨论】:
标签: ios afnetworking nsurlconnection