Reputation: 1
Lately i am sometimes getting this very strange message during operation of my app, with no crash. (mainly the app now just get images from the net ) . I thing it has to do with me drawing a rect around image , the message is :
<Error>: CGContextStrokeRect: 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.
the rect drawing :
-(UIImage*)imageWithBorderFromImage:(UIImage*)source
{
CGSize size = [source size];
UIGraphicsBeginImageContext(size);
CGRect rect = CGRectMake(0, 0, size.width, size.height);
[source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context,(255/255.f),(255/255.f),(255/255.f), 1.0);
CGContextStrokeRect(context, rect);
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return testImg;
}
Upvotes: 0
Views: 235
Reputation: 8202
You're [source size]
is possibly occasionally CGSizeZero
- put a check in and return nil
.
+(UIImage*)imageWithBorderFromImage:(UIImage*)source
{
CGSize size = [source size];
if (CGSizeEqualToSize(size, CGSizeZero)) {
return nil;
}
UIGraphicsBeginImageContext(size);
CGRect rect = CGRectMake(0, 0, size.width, size.height);
[source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context,(255/255.f),(255/255.f),(255/255.f), 1.0);
CGContextStrokeRect(context, rect);
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return testImg;
}
You can simiplify your above code by the way using the newer non-CG methods - this shows that you do not need to use CGContext
directly...
+(UIImage*)imageWithBorderFromImage:(UIImage*)source
{
CGSize size = [source size];
if (CGSizeEqualToSize(size, CGSizeZero)) {
return nil;
}
UIGraphicsBeginImageContext(size);
CGRect rect = CGRectMake(0, 0, size.width, size.height);
[source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];
[[UIColor whiteColor] setStroke];
UIRectFrame(rect);
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return testImg;
}
EDIT: I've also made your methods class methods as there is no reason to use an instance method - unless you want to call [source imageWithBoarder]
instead - in my opinion this is a bit neater :)
Upvotes: 1
Reputation: 640
The call to UIGraphicsGetCurrentContext()
is probably returning nil
.
As for your stated goal though, I think you would be better off skipping the CoreGraphics drawing route, and instead putting your UIImage
in a UIImageView
. If you're trying to give it a border, assign the UIImageView
's layer.borderWidth
and layer.borderColor
properties some values. Taken out of context though, it's tough to see what you're doing, so this may/may not be the solution to your problem.
Upvotes: 2
Reputation: 12979
I would highly discourage you from using the method you're using. Drawing images yourself is verbose, error prone and less maintainable. Instead, I recommend you use an UIImageView
. You can then place your drawing code within the UIImageView
's drawRect
method as described here. Otherwise, your graphics context will be nil
.
Performing drawing outside of the drawRect
method is highly discouraged.
Upvotes: 1