"Potential leak of an object" when using Core Graphics - ios

My app is drawing a shadow using the following code:
-(void)drawShadow:(CGContextRef)context rect:(CGRect)rect{
CGContextSaveGState(context);
//Set color of current context
[[UIColor blackColor] set];
//Set shadow and color of shadow
CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 3, [[UIColor colorWithWhite:0 alpha:0.5] CGColor]);
CGContextFillEllipseInRect(context, rect);
CGContextClipToMask(context, rect, CGBitmapContextCreateImage(context));
CGContextRestoreGState(context); // Warning shows in this line
}
When I run Product > Analyze, it marks the last instruction of this block with the message: "Potential leak of an object". When I remove that line, it shows the same message but in the closing bracket.
Any idea of what I'm doing wrong?
Thanks

I think it's the CGBitmapContextCreateImage that is leaking.
Try assigning it to a local variable and then calling CGImageRelease with it once you've clipped to mask.
Another iPhone - CGBitmapContextCreateImage Leak

chedabob's answer worked! Here's the final code:
-(void)drawShadow:(CGContextRef)context rect:(CGRect)rect{
CGContextSaveGState(context);
//Set color of current context
[[UIColor blackColor] set];
//Set shadow and color of shadow
CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 3, [[UIColor colorWithWhite:0 alpha:0.5] CGColor]);
CGContextFillEllipseInRect(context, rect);
CGImageRef bitmap = CGBitmapContextCreateImage(context);
CGContextClipToMask(context, rect, bitmap);
CGContextRestoreGState(context);
CGImageRelease(bitmap);
}

Related

Remove outside of bezier path

I have a function where I fill the image with a color and use a UIBezierPath to erase a point for corners.
CGRect rect = CGRectMake(0.0f, 0.0f, width, height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(context, kCGBlendModeCopy);
// Fill image
CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextFillRect(context, rect);
// Round corners
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:15.0];
CGContextSetStrokeColorWithColor(context, [[UIColor clearColor] CGColor]);
[bezierPath stroke];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
With the above, I get an image that does have the Bézier path cut out, and the background filled.
However, how can I remove the corners outside of the path, or get at least some way to reference where they are so I can clear them?
A couple of options:
Use CoreGraphics, like you have, but clip it to a path:
CGRect rect = CGRectMake(0.0f, 0.0f, width, height);
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(context, kCGBlendModeCopy);
// Round corners
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:15.0];
CGContextAddPath(context, bezierPath.CGPath);
CGContextClip(context);
// Fill image
CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Alternatively, eliminate CoreGraphics and just fill the UIBezierPath:
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0);
[[UIColor redColor] setFill];
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:15.0] fill];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Note, in both of those examples, I used UIGraphicsBeginImageContextWithOptions, supplying a scale of 0 (a scale optimized for display on the device in question). If you really want, you can supply a scale of 1, which obviously will be a bit pixelated when rendered on a retina device, but that's up to you.

Extracting CGColor out of UIColor

I have a UIColor initiated with [UIColor colorWithRed:Green:Blue:Alpha] and I want to extract its corresponding CGColor to use with CGContextSetFillColorWithColor.
I tried [[UIColor colorWithRed...] CGColor] but for some reason I get no coloring.I
[[UIColor colorWithRed:216 green:156 blue:1 alpha:1] CGColor]
The weird thing is that when I use:
[[UIColor orangeColor] CGColor]
It works just fine.
Any ideas?
Thanks!
EDIT:
The actual code:
CGRect rect = CGRectMake(0.0, 0.0, width, height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:216 green:156 blue:1 alpha:1].CGColor);
CGContextFillRect(context, rect);
self.image = UIGraphicsGetImageFromCurrentImageContext();
I don't think its code-related - again, if I change the UIColor to a constant color it all works.
The purpose of the code is to create an UIImage filled with a specific color.

What is wrong with my CGContextclip.

I am beginner with Core Garphics and I am try to get around with my understanding of CGContextclip. I am doing a simple program with custom UIButton drawrect as below
Rect dimension passed to drawRect method is CustomButton *button = [[CustomButton alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
#implementation CustomButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef colorRef = [[UIColor blueColor] CGColor];
CGContextSetStrokeColorWithColor(context, colorRef);
CGContextSetFillColorWithColor(context, [[UIColor blueColor] CGColor]);
CGContextSetLineWidth(context, 3.0f);
CGContextAddRect(context, rect);
CGContextStrokeRect(context, rect);
//CGContextFillRect(context, rect);
CGContextSaveGState(context);
CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextSetFillColorWithColor(context, [[UIColor redColor]CGColor]);
CGMutablePathRef rectPath = CGPathCreateMutable();
CGPathMoveToPoint(rectPath, NULL, CGRectGetMinX(rect), CGRectGetMidY(rect));
CGPathAddLineToPoint(rectPath, NULL, CGRectGetMaxX(rect), CGRectGetMidY(rect));
CGPathAddLineToPoint(rectPath, NULL, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
CGPathAddLineToPoint(rectPath, NULL, CGRectGetMinX(rect), CGRectGetMaxY(rect));
CGPathCloseSubpath(rectPath);
CGContextAddPath(context, rectPath);
CGContextClip(context);
CGContextStrokePath(context);
//CGContextFillPath(context);
CGContextRestoreGState(context);
CGColorRelease(colorRef);
}
#end
I am expecting a full rect with dimension (0,0,100,50) colored in blue and a half rect inside with red coloured. I am getting the blue colored rect but red colored rect is not visible in my context.
I have gone through some solution here and also at some blogs but to my faulty eyes the code looks simple and fine. What is wrong with my clipping.
Thank you for your time and response.
I think there's no path in the context anymore after you clipped it.Just fill another rectangle:
//CGContextFillPath(context);
CGContextFillRect(context, rect);

renderInContext not support mask?

I have a camera app to load photo into a device with mask. Everything is OK. When I try to use renderInContext to save the view to an image, I only see the image without any mask.
UIGraphicsBeginImageContext(contentView.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[contentView.layer renderInContext:context];
UIImage *outImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(outImage, self, #selector(image: didFinishSavingWithError: contextInfo:), context);
I have read some paper from Apple to say that renderInContext don't support mask and composition. I've made some search on the internet to get the information that UIView needed to draw as a context first and then use renderInContext to save the image.
Now my question is what method to do the job? What about drawRect, drawInRect, drawLayer, drawInContent, or other method. Can anyone give me a hint. Thanks a lots.
I started from here: http://chinkisingh.com/2013/03/03/draw-on-iphoneipad-screen-using-bezier-paths-core-graphics-ios-app-development/
I have a UIBezierPath and I wanted to apply a gradient to it and apply to an existing image, see if the following code helps
CGRect r = CGRectMake(0, 0, HEART_SIZE, HEART_SIZE);
UIBezierPath *heart = [Bezier heartShape:r]; //this is only in my case
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
UIColor *darkPink = [UIColor colorWithHue:0.915 saturation:1.000 brightness:0.941 alpha:1.000];
UIColor *lightPink = [UIColor colorWithHue:0.917 saturation:0.647 brightness:1.000 alpha:1.000];
NSArray *gradientColors = [NSArray arrayWithObjects:
(id)darkPink.CGColor,
(id)lightPink.CGColor,
(id)darkPink.CGColor, nil];
CGFloat gradientLocations[] = {0, 0.5, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
CGColorSpaceRelease(colorSpace);
UIGraphicsBeginImageContext(r.size);
heart.lineCapStyle = kCGLineCapRound;
heart.lineWidth = 10.0f;
[[UIColor blackColor] setStroke];
[[UIColor redColor] setFill];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
[heart stroke]; //removed the black stroke around
[heart fill];
CGContextAddPath(context, heart.CGPath);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, CGPointMake(10, 10), CGPointMake(210, 210), kCGGradientDrawsAfterEndLocation); //check that gradient is drawn in the right place
CGGradientRelease(gradient);
UIImage *theRightImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGContextSetFillColorWithColor not working with passed in color variable

I'm trying to color an image using CGContextSetFillColorWithColor and a color variable I'm grabbing from my Parse database.
Changing the color of the image this way works fine:
[...]
UIImage *image = [UIImage imageNamed:#"menuButton.png"];
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToMask(context, rect, image.CGImage);
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
[...]
That changes the menuButton.png to white no problem.
But when I try to change it to the hex color I'm getting from Parse (using a hex color utility) this way:
self.bgColor = [post objectForKey:#"bgColor"]; //bgColor = "#ffffff"
[...]
CGContextSetFillColorWithColor(context,
[[UIColor colorWithHexString:self.bgColor] CGColor]);
The image no longer appears. Is this a pointer issue? Thanks for your help.
I have had a similar problem and didn't find a direct solution. The solution for me was to use the -setFill method on my UIColor object, rather than using the intermediate CGColor object:
[[UIColor colorWithHexString:self.bgColor] setFill];

Resources