JPG图像不会加载UIImage imageNamed [英] JPG image doesn't load with UIImage imageNamed

查看:462
本文介绍了JPG图像不会加载UIImage imageNamed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了iOS版本4的引用+使用 imageNamed UIImage 对象的方法,文件扩展名不是必需的。

I read on the reference that from iOS version 4.+ with the method imageNamed of UIImage object, the file extension is not required.

来自 UIImage 类参考:


特殊注意事项。

Special Considerations.

在iOS 4及更高版本中,不需要文件名来指定
文件扩展名。在iOS 4之前,您必须指定文件名
extension。

On iOS 4 and later, the name of the file is not required to specify the filename extension. Prior to iOS 4, you must specify the filename extension.

但似乎这只适用于PNG文件。

But it seems that this only work with PNG files.

如果我的代码是:

[UIImage imageNamed:@"test"];




  • 仅当文件时才加载图像test.png

  • 如果图像是 test.jpg ,则不会加载图像。

    • The image loads only if the file is test.png
    • The image doesn't load if it's test.jpg.
    • 对我来说这是一个很大的问题,因为我需要保持动态图像加载(我不知道在运行时我想要加载的图像是 png jpg )。

      For me it is a big problem because I need to maintain a dynamic image loading (I do not know at runtime if the image I want to load is png or jpg).

      请帮助。我?

      谢谢。

      Please can you help me?
      Thanks.

      推荐答案

      最新的开发者参考说明了缺失的信息:

      The latest developer reference states the missing piece of information:


      特别注意事项
      在iOS 4及更高版本中,如果文件是PNG格式,则无需指定.PNG文件扩展名。在iOS 4之前,您必须指定文件扩展名。

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

      该库为PNG提供特殊处理的一个可能原因是iOS硬件针对PNG进行了优化。存储在应用程序包中的PNG图像由Xcode优化,改变PNG图像的字节顺序以匹配iPhone设备的图形芯片。 (请参阅此问题: PNG优先于JPEG对于iOS上的所有图像文件?)。

      One possible reason that the library gives PNGs special treatment, is that the iOS hardware is optimized for PNGs. PNG images stored in the application bundle are optimized by Xcode, changing the byte order of the PNG images to match the graphics chip of the iPhone device. (see this question: Is PNG preferred over JPEG for all image files on iOS?).

      如果您知道自己只有PNG或JPG,另一种解决方案是创建一个类别在UIImage上,如下所示。

      If you know that you will only have a PNG or a JPG, an alternative solution is to create a category on UIImage as per below.

      - (UIImage*) jgcLoadPNGorJPGImageWithName:(NSString*)name {
          UIImage * value;
          if (nil != name) {
              value = [UIImage imageNamed:name];
              if (nil == value) {
                  NSString * jpgName = [NSString stringWithFormat:@"%@.jpg", name];
                  value = [UIImage imageNamed:jpgName];
              }
          }
          return value;
      }
      

      这篇关于JPG图像不会加载UIImage imageNamed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆