Add text to CALayer : drawInRect not working - iphone

I did the following :
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
UIGraphicsPushContext ( ctx );
CGRect r = CGRectMake( 500, 300, 200, 100 );
NSString *text = [[NSString alloc] initWithString:#"raaaaaaaa!"];
UIColor *color = [ UIColor colorWithRed: (200.0f) green: (100.0) blue:(200.0f) alpha: 1.0f ];
[color set];
[ text drawInRect: r withFont:[UIFont systemFontOfSize:50] lineBreakMode: UILineBreakModeWordWrap alignment: UITextAlignmentLeft ];
[text release];
UIGraphicsPopContext();
}
}
}
The above code is not working. Why?
If i do the following instead, it is working:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
UIGraphicsPushContext ( ctx );
CATextLayer *label = [[CATextLayer alloc] init];
[label setFont:#"Didot"];
[label setFontSize:50];
[label setFrame:CGRectMake( 400, 300, 400, 100 )];
[label setString:#"Helloooooooooooooooooo"];
[label setAlignmentMode:kCAAlignmentCenter];
[label setForegroundColor:[[UIColor blueColor] CGColor]];
[layer addSublayer:label];
[label release];
UIGraphicsPopContext();
}
}
What is the difference and why drawInRect does nothing?
Thank you
EDIT
So, I was not using UIColor properly, so I changed
UIColor *color = [ UIColor colorWithRed: (200.0f) green: (100.0) blue:(200.0f) alpha: 1.0f ];
for this
UIColor *color = [UIColor colorWithRed:200 / 255.0 green:100 / 255.0 blue: 200 / 255.0 alpha: 1.0];
Still no luck, I can't see any text when using drawInRect

There are two questions in you question:
The above code is not working. Why?
At first I thought that the text didn't show up because it was drawn with white on white. While it may still have been so, that is not the only problem.
The rectangle that you are drawing your text into (r) is relative to the bounds of the layer you are drawing in. I.e. if you want to draw in the top left corner of the layer, the origin of the rect should be (0, 0). The above code is still not working though.
If you inspect the CGSize that drawInRect:withFont:... returns (the size of the rendered text) you can see that the size of the rendered text is 0 wide. This is an indication that the height of 100 for the rectangle to draw in is not enough. Increasing the height of the rectangle solves this and now the text is visible. If you are drawing some text inside a layer, maybe the most sensible rectangle to draw in is the bounds of the layer (CGRect r = [layer bounds];).
What I did to make it work:
Changed text color
Changed origin of r to (0,0)
Increased height of r
What is the difference [...] ?
In the first you are drawing text into the graphics context of the layer (what drawLayer:inContext: is supposed to do) but in the seconds example you are adding another layer to the hierarchy of the layer instead of drawing inside it. Every time your layer will redraw, a new text layer will be added to it. So nothing is actually drawn into the layers graphics context.
What happens next, after your layer has drawn itself (empty), is that the sublayers of your layers draw themselves and their content is composed on top of your layer. That is why you see the text of the text layer.
If you add a text layer like this you can make your code more efficient by not having to do any manual drawing. This requires that the text layer is added to your layer at some earlier point (only once) and that you don't do any custom drawing (it's often slow).

Only if you had read UIColor's class reference...
UIColor *color = [UIColor colorWithRed:200 / 255.0 green:100 / 255.0 blue: 200 / 255.0 alpha: 1.0];
will solve your problem.

I thing perhaps your frame's coordinate is wrong.
CGRect r = CGRectMake( 500, 300, 200, 100 );
[label setFrame:CGRectMake( 400, 300, 400, 100 )];
Note the iPhone screen size is 320*460 or 3
20*568(iPhone5). In your code you set the x coordinate 400 and 500 which is beyond the screen.
Note your coordinate should follow x<=320 or you can't see it in the screen unless you add this in a scrollview

Related

External Fill Color in iOS

I have two rectangles (two closed sub cgpaths).
Rectangle B is small and is present inside Rectangle A. All edges within it.
Is there a direct way to fill color area external to the rectangle B.
CAShapeLayer fillExternalColor something like that? If not a direct way, how to do this programmatically?
A - Purple color
B - Yellow color
So I tried drawing A and then drawing B. I wanted B to be of clear color (for now I put Yellow) but then I see purple color of A..
I found CGRectIntersection method that gives AnB and CGRectUnion method that gives AuB.
Is there a method that gives the rest of the area which is AuB - AnB?
You are using a CAShapeLayer. If your fillColor and your strokeColor are both opaque (alpha 1.0), you can simply set the layer's backgroundColor to fill those pixels that are within the layer's bounds but outside of its stroked and filled path.
My test code:
#implementation ViewController {
CAShapeLayer *layer;
}
- (void)viewDidLoad {
[super viewDidLoad];
layer = [CAShapeLayer layer];
layer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 250, 250)].CGPath;
layer.fillColor = [UIColor yellowColor].CGColor;
layer.strokeColor = [UIColor whiteColor].CGColor;
layer.lineWidth = 4;
layer.backgroundColor = [UIColor purpleColor].CGColor;
[self.view.layer addSublayer:layer];
}
- (void)viewDidLayoutSubviews {
layer.frame = self.view.bounds;
}
#end
Result:
I wanted to add red border inner rectangle on UIImage with masked outer part.
I came through this page. The following code is using CGContextEOFillPath can be helpful to others like me. (Some of the code is gathered from other pages.)
-(UIImage ) imageByDrawingBorderRectOnImage:(UIImage )image theRect:(CGRect)theRect
{
// begin a graphics context of sufficient size
UIGraphicsBeginImageContext(image.size);
// draw original image into the context
[image drawAtPoint:CGPointZero];
// get the context for CoreGraphics
CGContextRef ctx = UIGraphicsGetCurrentContext();
// set stroking color and to draw rect
[[UIColor redColor] setStroke];
// drawing with a red stroke color
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
// the line width to 3
CGContextSetLineWidth(ctx, 3.0);
// Add Stroke Rectangle,
CGContextStrokeRect(ctx, theRect);
// Now draw fill outside part with partial alpha gray color
// drawing with a gray stroke color
CGMutablePathRef aPath = CGPathCreateMutable();
// outer rectangle
CGRect rectangle = CGRectMake( 0, 0, image.size.width, image.size.height);
CGPathAddRect(aPath, nil, rectangle);
// innter rectangle
CGPathAddRect(aPath, nil, theRect);
// set gray transparent color
CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:0.75 green:0.75 blue:0.75 alpha:0.5].CGColor);
// add the path to Context
CGContextAddPath(ctx, aPath);
// This method uses Even-Odd Method to draw in outer rectangle
CGContextEOFillPath(ctx);
// make image out of bitmap context
UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();
// free the context
UIGraphicsEndImageContext();
return retImage;
}
Regards.
Assuming A is the bigger shape, B is the smaller, inner shape, and bounds refers to the containing rect of A (probably the bounds of the view)
Clip shape B to the context (-addClip should do it)
CGContextClearRect the bounds
Fill shape A
Edit, found the source of this code
Given HoleView : UIView you must set:
self.opaque = NO;
self.backgroudColor = [UIColor clearColor];
and in drawRect:
[[UIColor purpleColor] setFill];
UIRectFill(A);
CGRect gapRectIntersection = CGRectIntersection(B, A);
[[UIColor clearColor] setFill];
UIRectFill(gapRectIntersection);
kCAFillRuleEvenOdd
Specifies the even-odd winding rule. Count the total number of path crossings. If the number of crossings is even, the point is outside the path. If the number of crossings is odd, the point is inside the path and the region containing it should be filled.
Available in OS X v10.6 and later.
Declared in CAShapeLayer.h.
Refer to :CAShapeLayer Class reference

How can I replicate UINavigationBar's gradient colors?

I've been trying to replicate the gradient from UINavigationBar to use as a gradient on custom UIButton subclass objects on the same view.
However, I can't figure out how the colors are derived? That is, you only specify one color to set a UINavigationBar's background color- tintColor - but it creates a nice gradient with it seems at least 4 colors?
I'm really just interested in the "inner" top and bottom colors though - just inside the 1px border around the bar... the outer "border" colors do indeed appear different though.
EDIT - 1
Upon further research, it appears the HSB (instead of the RBG as first thought) values are being manipulated to get these different colors.
There is also a convenience method on UIColor to get the HSB values, which should be helpful:
getHue:saturation:brightness:alpha:
Helpful References Found So Far
HSL and HSV Wiki
UIColor Class Reference
Programmatically Lighten a Color
From the book Fundamentals of Interactive Computer Graphics
EDIT - 2
In case you were unaware that you could set a gradient for a background on a UIButton programmatically, here's some references for how to do such:
FUN WITH UIBUTTONS AND CORE ANIMATION LAYERS
Five Tips for Creating Stylish UIButtons (kudos to #cdo for providing this link)
EDIT - 3
I've put together a spreadsheet showing the original and the "inner" gradient colors (disregarding the outer most colors) in HSB values on UINavigationBar and its corresponding "back" button (titles are irrelevant and always displayed white).
Here's a link to the Google doc with the information that I've collected for a few sample colors:
https://docs.google.com/spreadsheet/ccc?key=0AnKVtzkNS9scdGVRN01pa1NQcC1hdThNbEVzQU8wRlE&usp=sharing
Note: these values were found by saving a screenshot using the retina, 3.5" iPhone Simulator (Xcode version 4.6) for iOS 6.1 and eye-dropping the HSB values using PhotoShop.
BOUNTY AWARD CRITERIA
I've opened a bounty on this question to bring more exposure to it and hopefully get a good answer. The answer that I'm looking for:
Provide a method of calculating/closely approximating (in most cases) the RGB or HSB values of the "inner top" and "inner bottom" gradient colors (see spreadsheet) created after setting a tintColor on UINavigationBar.
Bonus points awarded (above initial bounty offering) if you also provide a method for calculating the "inner top" and "inner bottom" gradient colors on the "back" button (which is similar to the navigation bar, but I've found these colors appear to be slightly "darker" usually)?
Short Answer: it is not gradient
Long Answer: After tint color applied, there is a transparent overlay image rendered on top of it.
It is called: UITintedTopBarHighlight#2x.png an it is in UIKit artwork. (uploaded here: http://cl.ly/image/2c2V3t1D1T3L)
It is 2x88 pixel image, and must be repeated horizontally over tinted background.
For back button, it is very similar, but there is also a mask to give it it's shape. UItintedBackButtonHighlight and UITintedBackButtonMask.
It's hard to copy the exact behavior because it seems that Apple is calculating different for different color groups. eg. a light color is slightly darkened while a dark color is lit up.
Same for the bar button item. For some colors the difference for normal "bordered" button and "done"-style button is completely different. Sometimes not noticeable like for turquoise but definitely seeable for orange.
For creating this sample code I used PaintCode a nice tool for prototyping btw..
Copy this code in a UIView subclass or something. Or just grab the pieces code you need.
- (void)drawRect:(CGRect)rect
{
//// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
//// Color Declarations
UIColor* tint = [UIColor colorWithRed: 1 green: 0.66 blue: 0.329 alpha: 1];
CGFloat tintRGBA[4];
[tint getRed: &tintRGBA[0] green: &tintRGBA[1] blue: &tintRGBA[2] alpha: &tintRGBA[3]];
UIColor* lighter = [UIColor colorWithRed: (tintRGBA[0] * 0.58 + 0.42) green: (tintRGBA[1] * 0.58 + 0.42) blue: (tintRGBA[2] * 0.58 + 0.42) alpha: (tintRGBA[3] * 0.58 + 0.42)];
CGFloat lighterRGBA[4];
[lighter getRed: &lighterRGBA[0] green: &lighterRGBA[1] blue: &lighterRGBA[2] alpha: &lighterRGBA[3]];
UIColor* lightest = [UIColor colorWithRed: (lighterRGBA[0] * 0.55 + 0.45) green: (lighterRGBA[1] * 0.55 + 0.45) blue: (lighterRGBA[2] * 0.55 + 0.45) alpha: (lighterRGBA[3] * 0.55 + 0.45)];
UIColor* darker = [UIColor colorWithRed: (tintRGBA[0] * 0.92) green: (tintRGBA[1] * 0.92) blue: (tintRGBA[2] * 0.92) alpha: (tintRGBA[3] * 0.92 + 0.08)];
CGFloat darkerRGBA[4];
[darker getRed: &darkerRGBA[0] green: &darkerRGBA[1] blue: &darkerRGBA[2] alpha: &darkerRGBA[3]];
UIColor* darkest = [UIColor colorWithRed: (darkerRGBA[0] * 0.65) green: (darkerRGBA[1] * 0.65) blue: (darkerRGBA[2] * 0.65) alpha: (darkerRGBA[3] * 0.65 + 0.35)];
//// Gradient Declarations
NSArray* gradientColors = [NSArray arrayWithObjects:
(id)lighter.CGColor,
(id)darker.CGColor, nil];
CGFloat gradientLocations[] = {0, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
//// top Drawing
UIBezierPath* topPath = [UIBezierPath bezierPathWithRect: CGRectMake(0, 0, rect.size.width, 1)];
[lightest setFill];
[topPath fill];
//// theGradient Drawing
UIBezierPath* theGradientPath = [UIBezierPath bezierPathWithRect: CGRectMake(0, 1, rect.size.width, rect.size.height - 1.0f)];
CGContextSaveGState(context);
[theGradientPath addClip];
CGContextDrawLinearGradient(context, gradient, CGPointMake(50, 1), CGPointMake(50, rect.size.height-1.0f), 0);
CGContextRestoreGState(context);
//// bottom Drawing
UIBezierPath* bottomPath = [UIBezierPath bezierPathWithRect: CGRectMake(0, rect.size.height-1.0f, rect.size.width, 1)];
[darkest setFill];
[bottomPath fill];
//// Cleanup
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
Thanks for the good question and the generous bounty. I went to work on this and neglected to check in to see that it was already answered acceptably. Nevertheless, it was fun building and testing the following navigation bar category, that reveals it's colors ...
//
// UINavigationBar+colors.h
#import <UIKit/UIKit.h>
#interface UINavigationBar (Colors)
// Answer an array of colors representing the color of the reciever, starting at fromY, up to toY
- (NSArray *)colorsFromY:(NSUInteger)fromY to:(NSUInteger)toY;
#end
Link with QuartzCore.framework.
//
// UINavigationBar+colors.m
#import "UINavigationBar+colors.h"
#import <QuartzCore/QuartzCore.h>
#define UIIMAGE_BYTES_PER_PIXEL 4u
#implementation UINavigationBar (Colors)
+ (NSData *)dataFromImage:(UIImage *)image {
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUInteger dataSize = height * width * UIIMAGE_BYTES_PER_PIXEL;
unsigned char *rawData = malloc(dataSize);
NSUInteger bytesPerRow = width * UIIMAGE_BYTES_PER_PIXEL;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
NSData *rtn = [NSData dataWithBytes:rawData length:dataSize];
free(rawData);
return rtn;
}
+ (UIColor *)colorOfImage:(UIImage *)image atX:(NSUInteger)px atY:(NSUInteger)py {
NSData *imgData = [self dataFromImage:image];
if (!imgData) return nil;
NSUInteger byteIndex = UIIMAGE_BYTES_PER_PIXEL * (image.size.width * py + px);
unsigned char rgbaData[4];
NSRange range = { byteIndex, 4u };
[imgData getBytes:rgbaData range:range];
CGFloat red = rgbaData[0] / 255.0;
CGFloat green = rgbaData[1] / 255.0;
CGFloat blue = rgbaData[2] / 255.0;
CGFloat alpha = rgbaData[3] / 255.0;
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
- (UIImage *)asImage {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
- (NSArray *)colorsFromY:(NSUInteger)fromY to:(NSUInteger)toY {
NSMutableArray *answer = [NSMutableArray array];
UIImage *image = [self asImage];
for (NSUInteger y = MAX(0, fromY); y < MIN(self.bounds.size.height, toY); y++) {
[answer addObject:[self.class colorOfImage:image atX:1 atY:y]];
}
return [NSArray arrayWithArray:answer];
}
#end
Call it like this:
// from a view controller contained by a navigation controller...
UINavigationBar *bar = self.navigationController.navigationBar;
NSArray *colors = [bar colorsFromY:0 to:bar.bounds.size.height];
for (UIColor *color in colors) {
NSLog(#"%#", color);
}
UIButton takes a single tintColor property but that doesn't mean it isn't computing other colors to use in the gradient behind the scenes. Try this tutorial.

How to create CALayer text glow effect

I am following Brad's answer to apply glow effect in my CALayer text.
Here is my code:
- (void)drawLayer:(CALayer *)theLayer inContext:(CGContextRef)context
{
UIGraphicsPushContext(context);
UIFont *font = [UIFont fontWithName:#"Verdana" size:11.0f];
CGRect rect = CGRectMake(theLayer.bounds.origin.x + 5, theLayer.bounds.origin.y + 5, theLayer.bounds.size.width - 10, theLayer.bounds.size.height - 10);
NSString * textToWrite = #"Some text";
UIColor *color = [ UIColor colorWithRed: (100.0f) green: (50.0) blue:(200.0f) alpha: 1.0f ];
CGContextSetFillColorWithColor(context, color.CGColor); //this has no effect!!!
CGContextSetShadowWithColor(context, CGSizeMake(0.0, 0.0), 2.0f, [UIColor greenColor].CGColor);
[textToWrite drawInRect:rect withFont:font
lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
UIGraphicsPopContext();
}
I am getting a decent green glow here. However I want the text to have its own color too, apart from glow. For this I am using color variable here, along with CGContextSetFillColorWithColor. But it seems to have NO effect. The text seems white, with green glow. I want text with main color = color and glow=green.
What should I do?
I'm confused by your color... I think when declaring red green and blue in objective-c you set values that are up to 1.0 being the maximum (like you did with alpha) so when you give them their true 255 hex value you then divide by 255. Your color should be white since all 3 values are so far above the maximum... Maybe I'm wrong though first try these two codes...
Replace your current FillColorWithColor code with this:
[[UIColor colorWithCGColor:color] set];
(or maybe this...)
[[UIColor colorWithCGColor:color.CGColor] set];
If they don't work then try them while also changing your color code to this:
UIColor *color = [ UIColor colorWithRed: (100.0f/255.0f) green: (50.0f/255.0f) blue:(200.0f/255.0f) alpha: 1.0f ];

Shadow not appearing for UIView using CALayer

I have a subclassed UIView loaded from a nib, and I cannot get a shadow to draw around it. I'm trying to get a shadow to appear around the entire view for quite some time now. I elected to place it in it's own sublayer to simplify animating it later. Here's the code:
-(void)awakeFromNib
{
self.clipsToBounds = NO;
// set up the shadow layer
CALayer *shadow = [CALayer layer];
shadow.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.bounds.size.width, self.bounds.size.height);
shadow.shadowColor = [UIColor blueColor].CGColor;
shadow.shadowRadius = 15.0;
shadow.opacity = 1.0;
[self.layer addSublayer:shadow];
// I set this property so I have access to it later to more easily animate it.
self.shadowLayer = shadow;
}
When I NSLog the shadowLayer property, the coordinates and frame are correct. It's matches the view it's backing.
I also set a border color and corner radius on self.layer and it appears correctly. If I put the shadow on self.layer it appears but it encompasses all the subviews of my parent UIView.
Any help is greatly appreciated.
Am assuming you have QuartzCore imported. I think you need to set & create a border to the UIView. The way to use this [self roundedLayerWithShadow:yourView.layer radius:5.0f];
- (void)roundedLayerWithShadow:(CALayer *)viewLayer
radius:(float)r
{
[viewLayer setMasksToBounds:YES];
[viewLayer setCornerRadius:r];
[viewLayer setBorderColor:[RGB(180, 180, 180) CGColor]];
[viewLayer setBorderWidth:1.0f];
[viewLayer setShadowColor:[RGB(0, 0, 0) CGColor]];
[viewLayer setShadowOffset:CGSizeMake(0, 0)];
[viewLayer setShadowOpacity:1];
[viewLayer setShadowRadius:2.0];
return;
}
I struggled with the same, and it turns out you need to set shadowOpacity to 1.0. In your code you accidentally use opacity instead of shadowOpacity. That's the same issue I had.
In general, for the shadow to appear:
the shadowOpacity should be larger than 0
the shadowRadius should be larger than 0
the masksToBounds should be set to false (to avoid the clipping of the shadow)
the shadowColor should be different than the background color of the view's superview
Swift 4.2. example implementation:
let myCustomView = MyCustomView()
myCustomView.layer.shadowColor = UIColor.black.cgColor
myCustomView.layer.shadowOpacity = 0.15
myCustomView.layer.shadowRadius = 5
myCustomView.layer.masksToBounds = false
myCustomView.layer.shadowOffset = CGSize(width: 0, height: 2)
Aside from borderRadius, your shadow layer looks very much transparent. Therefore it will not drop any shadow on shadow.superlayer.

Drawing a transparent circle on top of a UIImage - iPhone SDK

I am having a lot of trouble trying to find out how to draw a transparent circle on top of a UIImage within my UIImageView. Google-ing gives me clues, but I still can't find a working example.
Are there any examples that anyone knows of that demonstrate this?
Easiest way is simply to create a semi-transparent square UIView, then set the cornerRadius of its layer to be half of its width/height. Something like:
UIView *squareView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
squareView.alpha = 0.5;
squareView.layer.cornerRadius = 50;
...
[squareView release];
This has got to be the simplest solution:
CGFloat r = 150;
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,1.5*r,1.5*r)];
lbl.text = #"●";
lbl.transform = CGAffineTransformMakeTranslation(0.0f, -r/6);
lbl.textAlignment = UITextAlignmentCenter;
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor redColor];
lbl.font = [UIFont systemFontOfSize:2*r];
lbl.alpha = 0.5;
lbl.center = self.view.center;
[self.view addSubview:lbl];
One way would be to add a CAShapeLayer with a circular path, either directly to the layer of the UIImageView or as the layer of a new UIView that is added to the UIImageView.
If you actually want to modify the image, then create a mutable copy of it by drawing it into a CGBitmapContext then creating a new image from the modified bitmap.
CGPathRef circlePath = CGPathCreateMutable();
CGPathAddEllipseInRect( circlePath , NULL , CGRectMake( 0,0,20,20 ) );
CAShapeLayer *circle = [[CAShapeLayer alloc] init];
circle.path = circlePath;
circle.opacity = 0.5;
[myImageView.layer addSublayer:circle];
CGPathRelease( circlePath );
[circle release];
You can implement a custom sub-class of UIView that draws your image and then the circle in the drawRect method:
#interface CircleImageView : UIView {
UIImage * m_image;
CGRect m_viewRect;
// anything else you need in this view?
}
Implementation of drawRect:
- (void)drawRect:(CGRect)rect {
// first draw the image
[m_image drawInRect:m_viewRect blendMode:kCGBlendModeNormal alpha:1.0];
// then use quartz to draw the circle
CGContextRef context = UIGraphicsGetCurrentContext ()
// stroke and fill black with a 0.5 alpha
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 0.5);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.5);
// now draw the circle
CGContextFillEllipseInRect (context, m_viewRect);
}
You will need to set up the m_viewRect and m_image member functions on init.