将图像从URL保存到磁盘不起作用

时间:2015-05-07 16:35:39

标签: objective-c uiimage afnetworking afnetworking-2

我尝试从我的应用中的网址下载图片,以便今日推广可以使用它。 所以我打电话给[self downloadImageFromUrl:imageOne] ;,它使用这个方法: - (void)downloadImageFromUrl:(NSString *)url {     NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];     AFHTTPRequestOperation * requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];     requestOperation.responseSerializer = [AFImageResponseSerializer serializer];     [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * operation,id responseObject){         NSFileManager * fileManager = [NSFileManager defaultManager];         NSURL * storeUrl = [fileManager containerURLForSecurityApplicationGroupIdentifier:@" group.myapp.TodayExtensionDefaults"];         NSString * path = [storeUrl absoluteString];         path = [path stringByAppendingPathComponent:@" Test / imageOne.png"];         NSLog(@"路径是%@",路径);         NSLog(@"响应:%@",responseObject);         UIImage * image = responseObject;         [UIImagePNGRepresentation(image)writeToFile:path atomically:YES];     } failure:^(AFHTTPRequestOperation * operation,NSError * error){         NSLog(@"图像错误:%@",错误);     }];     [requestOperation start]; } 所以当我运行它时,一切都运行良好。我没有错。然而,当我转到模拟器中记录的文件夹时,没有文件存在。我从写入操作中得到的错误是写入返回错误:操作无法完成。 (可可错误4.)。所以我将写入操作更改为: if(![[NSFileManager defaultManager] fileExistsAtPath:path]){     NSError * error = nil;     if(![[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:& error]){         NSLog(@"错误:%@。%s,%i",error.localizedDescription,__ PRETTY_FUNCTION __,__ ____);     } } [UIImagePNGRepresentation(image)writeToFile:path options:NSDataWritingAtomic error:& error]; 但我仍然得到Write返回错误:操作无法完成。 (可可错误4.)

1 个答案:

答案 0 :(得分:2)

你正在抓住absoluteString(其中包括该计划):

NSString *path = [storeUrl absoluteString];

我相信你打算path

NSString *path = [storeUrl path];