gpt4 book ai didi

iOS UIImage 不显示 JPEG 图像

转载 作者:可可西里 更新时间:2023-11-01 05:09:24 24 4
gpt4 key购买 nike

我无法在我的 iOS 应用程序中显示某些图像。我已将一些图形从 PNG 更改为 JPG 文件格式,当我删除项目中已有的 PNG 图像并添加新的 JPG 时,模拟器仍显示旧的 PNG 图形。我在项目上运行了“clean”并重置了模拟器的内容,现在它根本不想加载图像 - [UIImage imageNamed:@"filename"] 返回 null。

另一方面,如果我将这些图形保存为 PNG,并将它们添加到项目中,而不是 JPEG,它可以很好地加载它们。但图像是照片,PNG 版本比 JPEG 大 7-8 倍,所以我真的很想弄清楚为什么它不会加载它们。

编辑:在查看文档后(好像是第 100 次),我意识到错误是我没有指定扩展名。 Supported Image Formats ( http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html ) 列出了很多受支持的格式,UIImage 引用说明:

On iOS 4 and later, if the file is in PNG format, it is not necessary to specify the .PNG filename extension.

我误读这意味着扩展名通常是可选的,但如果文件不是 PNG,则必须包含它。因此,如果文件名为 my_image.png,那么您可以将其作为 @"my_image"或 @"my_image.png"传递,但如果您有 my_image.jpg,则它只能作为 @"my_image.jpg"工作。

最佳答案

imageNamed方法:只加载项目中的*.png资源。尝试

  NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"imageName"] ofType:@"jpg"];
UIImage *theImage = [UIImage imageWithContentsOfFile:filePath];

关于iOS UIImage 不显示 JPEG 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17216210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com