【问题标题】:How to load 16 bits images with UIImage?如何使用 UIImage 加载 16 位图像?
【发布时间】:2012-03-02 06:27:20
【问题描述】:

我将优化我的应用程序的内存使用情况。 我的应用有许多带有图形的 UI,它们会占用大量设备内存。

我尝试使用 16 位图像而不是 32 位图像。 我的 OpenGL 纹理的内存消耗减少了。 但是,我发现 UIImage 保存的图像消耗的内存量与 32 位图像相同。

无论像素格式是 16 位还是 32 位,UIImage 是否使用每像素 8 位来存储图像? 或者我可以做些什么来减少 16 位图像的内存消耗??

非常感谢。

【问题讨论】:

  • 使用mac中可用的“pngcrush”。但是如果你这样做,你只能在iphone或ios中查看该图像。而不是在mac或任何其他系统中。它的优化是这样的。

标签: objective-c ios uiimage


【解决方案1】:

您可以使用 cgdata 提供程序

CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData((CFDataRef)[NSData dataWithContentsOfFile:@"some_file.png"]);

然后使用

CGImageRef CGImageCreate (
   size_t width,
   size_t height,
   size_t bitsPerComponent,
   size_t bitsPerPixel,
   size_t bytesPerRow,
   CGColorSpaceRef colorspace,
   CGBitmapInfo bitmapInfo,
   CGDataProviderRef provider,
   const CGFloat decode[],
   bool shouldInterpolate,
   CGColorRenderingIntent intent
);

函数,为尺寸、每像素位数设置所有适当的值,并使用在第一步中创建的数据提供程序。

然后通过[UIImage imageWithCGImage:]方法从CGImageRefs中获取UIImage。

【讨论】:

  • 谢谢。我从你的帮助下制作了一个 UIImage 并用它初始化了一个 UIImageView,并将它添加到我的主视图中。但是当系统渲染视图时,我的自动释放池崩溃了。该应用程序在“decode_data”行(CAlayer 渲染的内部函数调用)处崩溃,我将 NULL 传递给 CGImageCreate 的 decode[] 参数,这是文档建议的重量。你知道 wt 导致了崩溃吗?
  • 我猜一些搜索和尝试不支持 16 位。我不能用你的答案来解决(但它确实为我提供了非常有用的信息),所以我不能把它标记为正确。非常感谢。
猜你喜欢
  • 2020-05-07
  • 2020-01-26
  • 2011-01-18
  • 2018-07-30
  • 2017-09-03
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
相关资源
最近更新 更多