http://www.cnblogs.com/pengyingh/articles/2470257.html
// flip orientation CGContextTranslateCTM(context, 0.0, self.bounds.size.height); CGContextScaleCTM(context, 1.0, -1.0); // draw the image CGContextDrawImage(context, self.bounds, self.image.CGImage); // set the blend mode and draw rectangle on top of image CGContextSetBlendMode(context, kCGBlendModeColor); CGContextClipToMask(context, self.bounds, image.CGImage); // respect alpha mask CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0); CGContextFillRect(context, rect); } else { [self.image drawInRect:rect];
answer2:
You could try to add a mask to the view's Core Animation layer. CALayer *maskLayer = [CALayer layer]; [maskLayer setBounds:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)]; // Center the layer [maskLayer setPosition:CGPointMake([self view].bounds.size.width/2, [self view].bounds.size.height/2)]; // Set the cornerRadius to half the width/height to get a circle. [maskLayer setCornerRadius:50.f]; [maskLayer setMasksToBounds:YES]; // Any solid color will do. Just using black here. [maskLayer setBackgroundColor:[[UIColor blackColor] CGColor]]; // Set the mask which will only allow that which is in the // circle show through [[[self view] layer] setMask:maskLayer];