title: Clip UIImage with UIBezierPath Asynchronously
body: <p>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:</p>
  
  <pre><code>    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();
  </code></pre>
  
  <p>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.</p>