iOS - 使用AFNetworking获取图像可以保持缓存

时间:2014-08-06 10:57:57

标签: ios httprequest restkit afnetworking

在我的应用程序中我使用RESTKit,所以我的AFNetworking版本不是最新版本。我不确定如何查看它的版本。 我想从我的服务器下载图片,因为响应是jpg文件,我使用AFNetworking。在第一次下载图像时,它运行良好。然后我删除服务器上的图像并上传具有相同名称的新图像。然后,如果我删除应用程序中的图像并重新下载它。在这种情况下,我从第一次下载时仍然可以看到旧图片。

这是我的代码:

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://myserver.com"]];
[client setAuthorizationHeaderWithUsername:name password:password];

[client getPath:[NSString stringWithFormat:@"profile-images/%@.jpg", user.name] parameters:@{} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"SUCCES");
        NSData *imageData = responseObject;
        self.tmpImage = [UIImage imageWithData:imageData];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"FAIL");
        self.tmpImage = [UIImage imageNamed:@"myImage.png"];
    }];

在我看来,应用程序"记得"我下载图像时发送到服务器的第一个请求。然后当我重新下载它时,它给了我旧图片。有谁知道如何解决它?

2 个答案:

答案 0 :(得分:1)

我终于使用了NSMutableURLRequest。创建请求时,我将cachePolicy设置为NSURLRequestReloadIgnoringLocalCacheData。含义:request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;

以下是将请求发送到服务器的代码:

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://server.com"]];
[client setAuthorizationHeaderWithUsername:name password:password];

NSMutableURLRequest *request = [client requestWithMethod:@"GET" path:[NSString stringWithFormat:@"profile-images/%@.jpg", user.name] parameters:nil];

request.timeoutInterval = 10;
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;

AFImageRequestOperation *operation;
operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:^UIImage *(UIImage *image) {
        NSLog(@"block");
        return image;
} success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        NSLog(@"SUCCES");
        self.tmpImage = image;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
        NSLog(@"FAIL");
        self.tmpImage = [UIImage imageNamed:@"myImage.png"];
}];

[operation start];

答案 1 :(得分:0)

当您在服务器上上传图像时,首先在每次具有相同名称的文档目录中保存图像。在代码图像中,保存名称为@""Profile1.jpeg"

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];

    img = [self imageWithImage:img scaledToSize:CGSizeMake(70, 70)];

    [btn_Photo setImage:img forState:UIControlStateNormal];

    NSData *webData = UIImageJPEGRepresentation(img, 0.5);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    self.savedImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",@"Profile1.jpeg"]];

    [webData writeToFile:self.savedImagePath atomically:YES];

    [picker dismissViewControllerAnimated:YES completion:^{

        //[[UIApplication sharedApplication] setStatusBarHidden:YES];

    }];
}

使用新的ANetworking上传图片,如下面的代码New AFNetworking

AFHTTPRequestOperationManager *Manager = [AFHTTPRequestOperationManager manager];

[Manager POST:str_Submit parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
 {

        NSURL *filePath_Profile_Logo = [NSURL fileURLWithPath:self.savedImagePath];
        [formData appendPartWithFileURL:filePath_Profile_Logo name:@"userfile" error:nil];

 }
success:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     NSLog(@"Success");


 }
failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     NSLog(@"Fail")
 }];

请检查您的文档目录路径图像是否在上传前更改。 请