gpt4 book ai didi

iphone - 消除 UIImage imageNamed : FUD

转载 作者:行者123 更新时间:2023-12-03 18:08:13 25 4
gpt4 key购买 nike

2014 年 2 月编辑:请注意,这个问题可以追溯到 iOS 2.0!自那时起,图像要求和处理发生了很大变化。视网膜使图像更大并且加载它们稍微复杂一些。凭借对 iPad 和视网膜图像的内置支持,您当然应该在代码中使用 ImageNamed

我看到很多人说 imageNamed 不好,但也有同样数量的人说性能很好 - 特别是在渲染 UITableView 时。请参阅this SO question例如或 this article在 iPhoneDeveloperTips.com 上

UIImageimageNamed 方法曾经存在泄漏,因此最好避免这种情况,但已在最近的版本中修复。我想更好地了解缓存算法,以便就我可以信任系统在哪里缓存我的图像以及我需要在哪里加倍努力并自己做这件事做出合理的决定。我目前的基本理解是,它是由文件名引用的 UIImages 的简单 NSMutableDictionary 。它会变得更大,当内存耗尽时,它会变得更小。

例如,有人确定imageNamed后面的图像缓存不会响应didReceiveMemoryWarning吗?苹果似乎不太可能不这样做。

如果您对缓存算法有任何见解,请在此处发布。

最佳答案

tldr:ImagedNamed 很好。它可以很好地处理内存。使用它,不再担心。

2012 年 11 月编辑:请注意,这个问题可以追溯到 iOS 2.0!从那时起,图像要求和处理发生了很大变化。视网膜使图像更大并且加载它们稍微复杂一些。由于内置了对 iPad 和视网膜图像的支持,您当然应该在代码中使用 ImageNamed。现在,为了子孙后代:

sister thread Apple 开发论坛上的流量有所增加。具体Rincewind添加了一些权限。

There are issues in iPhone OS 2.x where the imageNamed: cache would not be cleared, even after a memory warning. At the same time +imageNamed: has gotten a lot of use not for the cache, but for the convenience, which has probably magnified the problem more than it should have been.

同时警告

On the speed front, there is a general misunderstanding of what is going on. The biggest thing that +imageNamed: does is decode the image data from the source file, which almost always significantly inflates the data size (for example, a screen sized PNG file might consume a few dozen KBs when compressed, but consumes over half a MB decompressed - width * height * 4). By contrast +imageWithContentsOfFile: will decompress that image everytime the image data is needed. As you can imagine, if you only need the image data once, you've won nothing here, except to have a cached version of the image hanging around, and likely for longer than you need it. However, if you do have a large image that you need to redraw often, then there are alternatives, although the one I would recommend primarily is to avoid redrawing that large image :).

With respect to the general behavior of the cache, it does cache based on filename (so two instances of +imageNamed: with the same name should result in references to the same cached data) and the cache will grow dynamically as you request more images via +imageNamed:. On iPhone OS 2.x a bug prevents the cache from being shrunk when a memory warning is received.

My understanding is that the +imageNamed: cache should respect memory warnings on iPhone OS 3.0. Test it when you get a chance and report bugs if you find that this is not the case.

所以,你已经明白了。 imageNamed:不会砸碎您的 window 或谋杀您的 child 。它非常简单,但它是一个优化工具。遗憾的是,它的名字很糟糕,而且没有任何类似的工具可以像它一样易于使用 - 因此人们过度使用它,并在它仅仅完成其工作时感到不安

我向 UIImage 添加了一个类别来解决这个问题:

// header omitted
// Before you waste time editing this, please remember that a semi colon at the end of a method definition is valid and a matter of style.
+ (UIImage*)imageFromMainBundleFile:(NSString*)aFileName; {
NSString* bundlePath = [[NSBundle mainBundle] bundlePath];
return [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", bundlePath,aFileName]];
}

Rincewind 还包含一些示例代码来构建您自己的优化版本。我不认为它值得维护,但这是为了完整性。

CGImageRef originalImage = uiImage.CGImage;
CFDataRef imageData = CGDataProviderCopyData(
CGImageGetDataProvider(originalImage));
CGDataProviderRef imageDataProvider = CGDataProviderCreateWithCFData(imageData);
CFRelease(imageData);
CGImageRef image = CGImageCreate(
CGImageGetWidth(originalImage),
CGImageGetHeight(originalImage),
CGImageGetBitsPerComponent(originalImage),
CGImageGetBitsPerPixel(originalImage),
CGImageGetBytesPerRow(originalImage),
CGImageGetColorSpace(originalImage),
CGImageGetBitmapInfo(originalImage),
imageDataProvider,
CGImageGetDecode(originalImage),
CGImageGetShouldInterpolate(originalImage),
CGImageGetRenderingIntent(originalImage));
CGDataProviderRelease(imageDataProvider);
UIImage *decompressedImage = [UIImage imageWithCGImage:image];
CGImageRelease(image);

此代码的权衡是解码图像使用更多内存,但渲染速度更快。

关于iphone - 消除 UIImage imageNamed : FUD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/924740/

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