gpt4 book ai didi

iphone - 释放 UIColor PatternImage 的正确方法

转载 作者:行者123 更新时间:2023-12-03 20:34:45 28 4
gpt4 key购买 nike

我的 iPad 应用程序中有一些全屏 UIColor PatternImages(在 scrollViews 中),并且遇到了一些内存问题(惊讶吗?)
当我开始出现内存问题时,我在 ScrollView 中实现了延迟加载。
当问题继续存在时,我放弃了工厂方法(如[UIColor colorWithPatternImage:...])“分配”方法(如[[UIColor alloc]initWithPatternImage:...]),这样我就可以通过释放页面来响应内存警告。

但是,每当我释放 UIColor PatternImages 时,我都会收到 “EXC_BAD_ACCESS” 错误。

起初我认为这可能是由我的 [UIImage imageNamed:...] 图案图像引起的,所以我切换到 [[UIImage alloc]initWithContentsOfFile:...] 图像,但这没有帮助。刚才我设置了 NSZombiesEnabled 它告诉我问题是:

-[UICGColor release]: message sent to deallocated instance 0x187b50

使用回溯:

#0  0x35823910 in ___forwarding___ ()
#1 0x35823860 in __forwarding_prep_0___ ()
#2 0x357e53c8 in CFRelease ()
#3 0x357e48de in _CFAutoreleasePoolPop ()
#4 0x3116532c in NSPopAutoreleasePool ()
#5 0x341a7508 in _wrapRunLoopWithAutoreleasePoolHandler ()
#6 0x3580ac58 in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#7 0x3580aacc in __CFRunLoopDoObservers ()
#8 0x358020ca in __CFRunLoopRun ()
#9 0x35801c86 in CFRunLoopRunSpecific ()
#10 0x35801b8e in CFRunLoopRunInMode ()
#11 0x320c84aa in GSEventRunModal ()
#12 0x320c8556 in GSEventRun ()
#13 0x341dc328 in -[UIApplication _run] ()
#14 0x341d9e92 in UIApplicationMain ()
#15 0x00002e5e in main (argc=1, argv=0x2fdff610) at...

我也没有任何 UICGColor 对象,所以我认为我的“分配”的 UIColors 具有底层 UICGColor自动释放对象...?有什么想法/见解吗?

最佳答案

我也遇到了同样的问题,这就是我优化代码的方法。

在 Instruments 中进行一些分析和调试后,我发现 colorwith 模式将占用 1.12 mb,并且有一个名为 ripl_create 的负责库。对于带有 colorWithPattern 的每个屏幕,将占用相同的 1.12,因此您将分配多个 oj 1.12 mb。这糟透了我的应用程序。所以我决定不使用 colorWithPattern。

我猜你想将图像设置为 View 的背景。我建议保留一个 ImageView 并将图像放置到其中...

现在进行 imageView 优化

如果您希望该图像在应用程序的许多部分中使用,或者包含该图像的 View 将被频繁访问,那么请使用 imagenamed。

imageNamed 会缓存图像,即使你将其置空或释放也不会释放它。

在另一种情况下,您希望发布图像

在viewWillAppear或viewDidLoad中将图像分配给imageview

        NSString *fileLocation = [[NSBundle mainBundle] pathForResource:@"yourImage" ofType:@"png"];
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:fileLocation]];

在你的viewDidDisappear中设置nil来释放图像

       imageView.image=nil;

关于iphone - 释放 UIColor PatternImage 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4795470/

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