Domanda

I have a CALayer object called sublayer. I use it as self.sublayer throughout my view controller because I have made it a property in my view controller's header file.

I set the sublayer's contents property equal to a UIImage object that is created using a CGImageRef object called imageRef:

self.subLayer.contents = (id)[UIImage imageWithCGImage:imageRef].CGImage;

I then release the imageRef object right away now that it has been used to create the sublayer contents and it is no longer needed:

CGImageRelease(imageRef);

However, here is what is bothering me. Later on in the code I will no longer need self.sublayer.contents and I want to make sure I release the CGImage it contains properly.

If I NSLog self.sublayer.contents it will print this to the console: <CGImage 0x146537c0>

So I need to be able to release this CGImage as well.

I tried using this to release the CGImage, but the NSLog still prints the same to the console:
CGImageRelease((__bridge CGImageRef)(self.subLayer.contents));

If I use this, the NSLog will print to the console as (null), but I am worried that this is technically not releasing the CGImage:

self.subLayer.contents = nil;

Does setting the sublayer's contents property to nil properly release the CGImage, or am I correct in thinking that it is not technically releasing the CGImage?

I am experiencing memory problems right now in my app so I need to make sure that I am releasing this CGImage properly.

È stato utile?

Soluzione

The contents property on CALayer is a retaining property, meaning that it's setter implementation more or less does this:

- (void)setContents:(id)contents
{
    if (contents == _contents) return; // Same as existing value
    [_contents release];
    _contents = [contents retain];
}

So, when you set nil as the new contents, the old contents is released.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top