【问题标题】:How can I convert imageNamed to imageWithContentsOfFile?如何将 imageNamed 转换为 imageWithContentsOfFile?
【发布时间】:2014-03-10 14:49:30
【问题描述】:

我尝试将imageNamed: 转换为imageWithContentsOfFile:,但每次都失败了。我看不到细胞上的图片。

UIImage *image = [UIImage imageNamed:[cellCountry objectForKey:@"Country_Flag"]];
cell.imageView.image = image;

[cellCountry objectForKey:@"Country_Flag"] 持有 flag1@2x.png

图片文件的路径是/Resources/Images

这是我的尝试。

UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[cellCountry objectForKey:@"Country_Flag"] ofType:@"png"]];
cell.imageView.image = image;

如何转换它?我在等你的帮助...

【问题讨论】:

  • 发布您使用imageWithContentsOfFile:的尝试。
  • 我忘了添加我的尝试。对此感到抱歉。我加了。

标签: ios objective-c uiimage


【解决方案1】:

您尝试使用imageWithContentsOfFile 已接近成功。由于来自[cellCountry objectForKey:@"Country_Flag"] 的值已经有文件扩展名,所以在获取路径时也不能指定扩展名。

试试:

UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[cellCountry objectForKey:@"Country_Flag"] ofType:nil]];

【讨论】:

    【解决方案2】:

    您应该尝试以下方法

    [UIImage imageWithContentsOfFile:[
        [NSBundle mainBundle] pathForResource:@"mainScreenBackground" ofType:@"png"]]
    

    【讨论】:

    • 您的NSData 示例存在严重问题。
    • 最好调整您的示例以适应 OP 在他们的问题中的内容。
    • 第二个例子的问题能不能详细说明一下,我顺便删一下!
    • 您的 NSData 示例并非有效代码。
    • @MariusFalkenbergWaldal 实际上,当我注意到那个人的用户名时,我正在输入那个并且正要提交它。我的意思是取消,但无论如何还是提交了它。 :)
    猜你喜欢
    • 2014-03-16
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多