2
4

2 回答 2

3

Me too had the same problem and this is how i optimized my code.

After some analysis in Instruments and debugging i found out that colorwith pattern will occupy 1.12 mb with a responsible library called ripl_create. For each screen with colorWithPattern will occupy the same 1.12 and so you will have multiples oj 1.12 mb allocated. This sucked my app.so i decided No colorWithPattern.

I guess you want to set image to the background of a view. What i would suggest is keep a ImageView and place the image to it...

Now Coming to imageView optimization

If you want the image to be used in many part of the app or the view that contains the image will be frequently visited then go for imagenamed.

imageNamed will cache the image and will not release it even if you nil it or release it.

On the other case where you wwant image to be released

In you viewWillAppear or viewDidLoad assign the image to imageview

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

inYour viewDidDisappear set nil to release the image

       imageView.image=nil;
于 2011-01-25T16:20:56.850 回答
2

OK, I have worked something out, thanks in part to the (poorly tagged and titled) SO question/answers here. What I needed to do was read-up about UIView.backgroundColor. In my docs it says @property(nonatomic, copy) UIColor *backgroundColor. The "copy" here tells me that when I say something like:

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

The myUIView.backgroundColor is actually a different object to the [[UIColor alloc]initWithPatternImage:myUIImage]. This separate myUIView.backgroundColor is, in fact, autoreleased, so when I go [myUIView.backgroundColor release]; I am actually shooting myself in the foot. What I need to do is:

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

(and of course, around that, I'm also allocing and releasing myUIImage). Now to release my scrollView patternImage, I can just do something like currentPage.backgroundColor=nil; (like @BuildSucceeded) or currentPage.backgroundColor=[UIColor clearColor]; (not sure if there is a practical difference). I guess internally, both of these cause currentPage to autorelease the old backgroundColor. Since making these changes, I haven't been able to get my app to crash.

I need to say this: I don't think C++ does it quite right, but C# and Java both manage quite nicely without any of this complicated release, autorelease, @property business. Of course, if someone wanted to implement an analogous (to Objective-C) pointer management system in Java or C# they could, but of course no one wants this because it would be pretty useless. Pleeeease Apple, migrate away from Objective-C!

于 2011-01-26T03:53:56.463 回答