ios - 为什么我的图像无法写入文件和显示?

标签 ios objective-c iphone

我想同时下载、保存和显示图像。

我正在使用这段代码,当我直接加载 *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/

相关文章:

ios - UISearchController 重叠 TableView 的 ScrollView 。解决这个问题的最佳方法是什么?

objective-c - iPhone/iPad 同时平移、捏合和旋转 View

iphone - UIView removeFromSuperView 动画延迟

iphone - 确定 iPhone 互联网连接是否可用的最简单方法?

ios - TextView及其内容

ios - Apache 在 Vagrant 中创建/编写目录

ios - 更新单元格 subview 的约束

iphone - 使用 UIActivityViewController 感到困惑

ios - iPhone 6 Plus 在 accessoryView 旁边的 UITableViewCell 中显示奇怪的线条

ios - Dropbox 错误 - 'uploadData:mode:autorename:clientModified:mute:propertyGroups:inputData:'