正确的方法来释放UIColor PatternImage

时间:2011-01-25 15:40:30

标签: iphone uiimage exc-bad-access cgimage uicolor

我的iPad应用中有一些全屏UIColor PatternImagesscrollViews},并且遇到了一些内存问题(惊喜?)
当我开始遇到内存问题时,我在滚动视图中实现了延迟加载 当问题继续存在时,我离开了工厂方法 (如[UIColor colorWithPatternImage:...]) “分配”ed方法 (如[[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个对象,所以我想我的“alloc”ed UIColors有某种基础UICGColor自动释放对象......?任何想法/见解?

2 个答案:

答案 0 :(得分:3)

我也有同样的问题,这就是我优化代码的方式。

经过仪器和调试的一些分析后,我发现colorwith模式将占用1.12 mb,并使用一个名为ripl_create的负责库。对于每个具有colorWithPattern的屏幕将占用相同的1.12,因此您将分配多个oj 1.12 mb。这吸了我的app.so我决定没有colorWithPattern。

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

现在开始进行imageView优化

如果您希望图像在应用的许多部分中使用,或者经常访问包含图像的视图,请转到图像名称。

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

在另一个你想要发布图像的情况下

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

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

inYour viewDidDisappear设置为nil以释放图像

       imageView.image=nil;

答案 1 :(得分:2)

好的,我已经解决了一些问题,部分归功于(标记不清,标题很差)SO问题/答案here。我需要做的是阅读UIView.backgroundColor。在我的文档中,它说@property(nonatomic, copy) UIColor *backgroundColor。这里的“副本”告诉我,当我说出这样的话时:

myUIView.backgroundColor=[[UIColor alloc]initWithPatternImage:myUIImage];

myUIView.backgroundColor实际上是与[[UIColor alloc]initWithPatternImage:myUIImage]不同的对象。事实上,这个单独的myUIView.backgroundColor是自动释放的,所以当我去[myUIView.backgroundColor release];时,我实际上是在脚下射击。我需要做的是:

UIColor* tmpColor=[[UIColor alloc]initWithPatternImage:myUIImage];
currentPage.backgroundColor=tmpColor;
[tmpColor release];

(当然,在那之前,我也在分配和发布myUIImage)。现在要发布我的scrollView patternImage,我可以像currentPage.backgroundColor=nil;(如@BuildSucceeded)或currentPage.backgroundColor=[UIColor clearColor];那样做(不确定是否存在实际差异)。我猜在内部,这两个原因导致currentPageautoreleasebackgroundColor。自从进行这些更改后,我无法让我的应用程序崩溃。

我需要这样说:我不认为C ++做得很好,但是C#和Java在没有任何复杂版本,自动发布,@ property业务的情况下都能很好地管理。当然,如果有人想用Java或C#实现一个类似的(到Objective-C)指针管理系统,他们可以,但当然没有人想要这个,因为它会毫无用处。 Pleeeease Apple,远离Objective-C!