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

时间:2015-04-08 10:35:31

标签: 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];
    }];

1 个答案:

答案 0 :(得分:4)

我认为错误就行了:

UIImage * imag2e = [UIImage imageNamed: @"Image"];

+[UIImage imageNamed:]的文档中,它声明:

  

如果这是第一次加载图像,该方法会在应用程序的主包中查找具有指定名称的图像。

保存的图像位于其他位置。例如。尝试:

UIImage * imag2e = [UIImage imageWithContentsOfFile: filePath];