Clip UIImage with UIBezierPath Asynchronously
asynchronous, ios, uibezierpath, uigraphicscontext, uiimage
Solution
You are doing more work than you need to. The "invalid context" messages are caused by calling `[path addClip]` before calling `UIGraphicsBeginImageContextWithOptions()`. The `UIBezierPath` tries to access the thread's current graphics context, but you haven't set one up yet.
Here's a simpler way to get the same results. Note that you don't need to use `CGContext` at all.
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(self.cornerRadius, self.cornerRadius)];
[path addClip];
[_image drawInRect:rect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Problem
I want to add rounded corners to an UIImageView without using QuartzCore to avoid performance issues in a UIScrollView so I solved it like that: ``` UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(self.cornerRadius, self.cornerRadius)]; [path addClip]; UIGraphicsBeginImageContextWithOptions(rect.size, NO, [[UIScreen mainScreen] scale]); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetBlendMode(UIGraphicsGetCurrentContext( ),kCGBlendModeClear); CGContextSetStrokeColorWithColor(context, [UIColor clearColor].CGColor); CGContextAddPath(context,path.CGPath); CGContextClip(context); CGContextClearRect(context,CGRectMake(0,0,width,height)); [_image drawInRect:rect]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); ``` Sadly, called in drawRect, this takes a little bit of processing time which produces a lag while scrolling in a UIScrollView. Hence I tried to process this in a seperate thread with the help of a dispatch_async. This removed the lag and everything works smoothly like it should be. But now I have another problem. I get many many invalid context messages in the debugger because the GraphicsContext is not always present when the thread starts the image processing asynchronously. Is there a way to process the rounded corners in my Image without getting invalid context messages? Please note that I do not want to use QuarzCore's cornerRadius or mask-functions.