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();