我想同时下载、保存和显示图像。
我正在使用这段代码,当我直接加载 *image 时它确实有效。
[self.imageView setImageWithURLRequest:request placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
// Set the image
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"];
// Save image.
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
UIImage * imag2e = [UIImage imageNamed: @"Image"];
weakSelf.imageView.image = imag2e;
[self updateZoom];
[self updateConstraints];
[MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"An error occured when loading the image, %@", [error description]);
[MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
}];
最佳答案
我觉得错误就在于此:
UIImage * imag2e = [UIImage imageNamed: @"Image"];
在 +[UIImage imageNamed:] 的文档中它指出:
If this is the first time the image is being loaded, the method looks for an image with the specified name in the application’s main bundle.
而保存的图像位于其他地方。例如。尝试:
UIImage * imag2e = [UIImage imageWithContentsOfFile: filePath];
关于ios - 为什么我的图像无法写入文件和显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29512304/