0

I was watching an Apple video from last regarding drawing in Quartz. The speaker said that you should store your CGGradientRef in an ivar so the system can take care of caching. So my first question is, how does that work? Is it just because the gradient gets created once for that object and then never gets created again unless the object gets created again?

Also, where do you release the gradient? Do you release it like other ivars in dealloc?

.h
@property (nonatomic, readonly) CGGradientRef gradient;

.m
- (CGGradientRef)gradient {
    if (NULL == _gradient) {
        CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
        _gradient = CGGradientCreateWithColors(rgb, (CFArrayRef)[NSArray arrayWithObjects:(id)[[UIColor colorWithRed:28./255 green:160./255 blue:255./255 alpha:1] CGColor], (id)[[UIColor colorWithRed:11./255 green:67./255 blue:107./255 alpha:1] CGColor], nil], nil);
        CGColorSpaceRelease(rgb);
    }
    return _gradient;
}

Where do I release the CGGradientRef? Thanks!

Crystal
  • 28,460
  • 62
  • 219
  • 393

1 Answers1

0

With a simple search i found this:

https://stackoverflow.com/a/8032905

Add this before the return:

[(id)result autorelease];
Community
  • 1
  • 1
Luis Ascorbe
  • 1,985
  • 1
  • 22
  • 36