开发者

Memory issue in iPhone

I am trying to free up memo开发者_如何学运维ry in objective-C. Also I am new to it.

Please find below the sample code. What do I have to do to free up in this code:

   CGImageRef cgiRef = [uiiInputImage CGImage];
   CGImageRef cgiNewRef = CGImageCreateWithImageInRect(cgiRef, cgrRegionInfo);
   uiiTargetTemp = [UIImage imageWithCGImage:cgiNewRef];
   UIImage *uiiOutputImage = uiiTargetTemp;
   uiiTargetTemp = nil;

Also where can I find documentation regarding the flow of allocating, assigning and releasing objects in Objective-C


To precisely answer your question:

CGImageRef cgiRef = [uiiInputImage CGImage];
CGImageRef cgiNewRef = CGImageCreateWithImageInRect(cgiRef, cgrRegionInfo);
uiiTargetTemp = [UIImage imageWithCGImage:cgiNewRef];
UIImage *uiiOutputImage = uiiTargetTemp;
uiiTargetTemp = nil;
CGImageRelease(cgiRef); //added

//....
//later on sometime you need to release the following
[uiiInputImage release];
[uiiOutputImage release];
CGImageRelease(cgiNewRef);

Judging from this snippet, you would be well served by reading the beginner resources provided by Apple. In addition to the resources @Swapna provided, I would recommend The Objective-C Programming Language: Introduction and Memory Management Programming Guide: Introduction. A good book or two also can't hurt. ;)


CGImageRef cgiRef = [uiiInputImage CGImage];
CGImageRef cgiNewRef = CGImageCreateWithImageInRect(cgiRef, cgrRegionInfo);
uiiTargetTemp = [UIImage imageWithCGImage:cgiNewRef];
UIImage *uiiOutputImage = uiiTargetTemp;
uiiTargetTemp = nil;
CGImageRelease(cgiNewRef);

You have to release what you received from CGImageCreateWithImageInRect, hence CGImageRelease.

This is not complete.I have some doubt about reference returned by imageWithCGImage, whether we should release it or not..Documentation is not clear about that one.

Return Value

A new image object for the specified Quartz image, or nil if the method could not initialize the image from the specified image reference.

Discussion

This method does not cache the image object. You can use the methods of the Core Graphics framework to create a Quartz image reference.

..See this and this thread discussing about it.. This is apples official documentation about memory management and this tutorial is helpful..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜