I am a beginner in iOS programming, so sorry if my question is a stupid question.
I am trying to make an app which performs custom drawing on a loaded image.
To do it, I found out that a solution is to subclass UIView
and edit the drawRect
method.
I made that on the following code which activates on an IBAction
linked to a button in the Interface Builder storyboard file.
UIImageView *image = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"SampleImage.jpg"]];
image.frame = previewView.frame;
[image setContentMode:UIViewContentModeScaleAspectFit];
[previewView addSubview:image];
customView *aCustomView = [[customView alloc] initWithFrame: CGRectMake(image.bounds.origin.x, image.bounds.origin.y, image.bounds.size.width, image.bounds.size.height)];
[previewView addSubview:aCustomView];
customView
is the UIView
subclass that I created, whose init
and drawRect
methods are set like this:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
NSLog(@"INITIALIZING");
if (self) {
// Initialization code
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
NSLog(@"DRAWING");
CGContextClearRect(ctx, rect);
CGContextSetRGBFillColor(ctx, 0, 255, 0, 0.3);
CGContextFillEllipseInRect(ctx, rect);
CGContextSetRGBStrokeColor(ctx, 255, 0, 0, 1);
CGContextSetLineWidth(ctx, 2);
CGContextStrokeEllipseInRect(ctx, rect);
}
The problem that I have is that no drawing is made and on NSLog
I have the "INITIALIZING" message, but not the "DRAWING" drawing.
So basically it makes the initWithFrame
, but it doesn't call the drawRect
method.
Could you please point me what am I doing wrong?