【问题标题】:AFNetworking get image if it is modifiedAFNetworking 获取图像,如果它被修改
【发布时间】: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


    【解决方案1】:

    我在我的应用程序中使用#import "UIImageView+AFNetworking.h" 类别将图像从我的服务器加载到应用程序。它工作得很好,每当对服务器上的图像进行更新时,它都会生成新的 URL,因此当我使用新的 URL 请求时,AFNetworking 将找不到缓存的图像并从服务器加载新的图像。

    您还应该检查这一点,How do I get cached image stored by AFNetworking's UIImageView category? - 当您需要在应用的缓存区域中查看图像时,就会出现要求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      • 2019-02-27
      • 1970-01-01
      • 2018-07-31
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多