0

I have a UIImageView within an UIViewController and want to add a simple image on the existing imageView.

The viewDidLoad-method looks like this:

    UIImage *test;
    test = [UIImage imageNamed:@"position-dot.png"];

    [test drawAtPoint:CGPointMake(100,100) ];

A part of the error-message I receive look like this:

CGContextSaveGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

CGContextSetBlendMode: invalid context 0x0. This is a serious error.

I tested to reset my iOS Simulator (solution in other threads about this), but that didn't fix the problem.

5
  • You want to display the UIImage on a UIImageView or render it on top of what is currently displayed by it?
    – Alladinian
    Commented Apr 2, 2014 at 16:33
  • 1
    You should be able to resolve this by wrapping your code in calls to UIGraphicsBeginImageContext() and UIGraphicsEndImageContext().
    – user1078170
    Commented Apr 2, 2014 at 16:38
  • @Alladinian: The second alternative. On top of what is currently displayed by it.
    – iHank
    Commented Apr 2, 2014 at 16:41
  • geraldWilliam, do you have an example?
    – iHank
    Commented Apr 2, 2014 at 16:41
  • Is using a second UIImageView an option for you? Commented Apr 2, 2014 at 16:48

1 Answer 1

1

While the approach in my above comment does silence the warning, I was finding that the image was not displaying as I expected. I found that the easiest way to make drawAtPoint work was to subclass UIView and put the exact code that you have into the implementation of drawRect: like so.

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    UIImage *test;
    test = [UIImage imageNamed:@"position-dot"];
    [test drawAtPoint:CGPointMake(100.0, 100.0)];
}

Then, in your controller:

MyViewSubclass *view = [[MyViewSubclass alloc] initWithFrame:self.view.frame];
[self.view addSubview:view];
2
  • Thanks, that did show the image. But the image that it is supposed to be on top is now black. It is black even if I add it as a sub view to the ImageView (the imageview that now is black). Why is that?
    – iHank
    Commented Apr 2, 2014 at 17:54
  • Oh right. Essentially, both images need to be drawn into the same view. See this forum answer [iphonedevsdk.com/forum/iphone-sdk-development/… from @DuncanC for an example of how to implement a "watermarked" image.
    – user1078170
    Commented Apr 2, 2014 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.