objective-c - 如何从 NSHomeDirectory 加载图像

标签 objective-c ios uiimage save

所以,我目前通过这种方法将图像保存在我的 UIImageView 中:

- (void)applicationDidEnterBackground:(UIApplication*)application {

    NSString  *image1 = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image1.png"];
    NSString  *image2 = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image2.png"];
}

图像保存得很好,但我不知道如何在 viewDidLoad 中设置 UIImageViews 图像。这是我一直在尝试的:

- (void)viewDidLoad
{
    self.imageView.image =[UIImage imageNamed:[(NSHomeDirectory *)Documents objectForKey:@"image1"]];

    self.imageView2.image =[UIImage imageNamed:[(NSHomeDirectory *)Documents objectForKey:@"image1"]];
}

但是,显然那是行不通的。我无法理解这里的基础知识。任何帮助都会很棒,谢谢!

最佳答案

-imageNamed: 方法在应用程序的主包中查找命名图像是否尚未缓存。来自文档:

The name of the file. 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 class reference)

您需要 -imageWithContentsOfFile: 代替。

关于objective-c - 如何从 NSHomeDirectory 加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10111984/

相关文章:

ios - AudioConverter#FillComplexBuffer 返回 -50 并且不转换任何内容

UIImage imageWithContentsOfFile : not working on iOS8

ios - 如何找出给定 UIImage 大小的最大可能文件大小是多少?

objective-c - 查看 Objective-c 的汇编代码

objective-c - CollectionView iOS 7 问题

ios - 在没有macOS的情况下开发IOS应用

ios - 爸爸为 React Native 解析 CSV

swift - 将 CALayer 作为子层添加到 UIImage 时出现意外行为

ios - dismissViewControllerAnimated : not animating

ios - 在 UIBarButtonItem 上添加手势