【问题标题】:How To Load Images From NSHomeDirectory如何从 NSHomeDirectory 加载图像
【发布时间】:2012-04-11 18:40:44
【问题描述】:

所以,我目前正在通过这种方法将图像保存在我的 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"]];
}

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

【问题讨论】:

    标签: objective-c ios uiimage save


    【解决方案1】:

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

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

    (见UIImage class reference

    你想要-imageWithContentsOfFile:

    【讨论】:

    • 非常感谢。那么,我的 viewDidLoad 是否应该像这样:NSString *image1String = [[NSBundle mainBundle] pathForResource:@"Documents/image1" ofType:@"png"];self.imageView = [UIImage imageWithContentsOfFile:image1String];?我收到一条警告“从 UIImage 分配 UIImageView* 的指针类型不兼容”。
    • 你已经接近了。您需要从您刚刚加载的UIImage 创建一个UIImageView 对象-imageWithContentsOfFile:。所以:self.imageView = [[[ UIImageView alloc ] initWithImage: [ UIImage imageWithContentsOfFile: imagePath ]] autorelease ];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    • 2013-03-16
    • 2018-12-29
    • 2012-07-20
    相关资源
    最近更新 更多