以前,写过一篇文章:图片圆角显示,这篇文章中的方法,可以将图片的四个角设置成圆角显示,如果将cornerRadius的值设置为图片宽度的一半,显示的则为圆图,本文提供了另一种将方图转成圆图的实现。
核心代码如下:
- (void)drawRect:(CGRect)rect { // Image rect CGRect imageRect = CGRectMake((self.borderWidth), (self.borderWidth), rect.size.width - (self.borderWidth * 2), rect.size.height - (self.borderWidth * 2)); // Start working with the mask CGColorSpaceRef maskColorSpaceRef = CGColorSpaceCreateDeviceGray(); CGContextRef mainMaskContextRef = CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, 8, rect.size.width, maskColorSpaceRef, 0); CGContextRef shineMaskContextRef = CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, 8, rect.size.width, maskColorSpaceRef, 0); CGColorSpaceRelease(maskColorSpaceRef); CGContextSetFillColorWithColor(mainMaskContextRef, [UIColor blackColor].CGColor); CGContextSetFillColorWithColor(shineMaskContextRef, [UIColor blackColor].CGColor); CGContextFillRect(mainMaskContextRef, rect); CGContextFillRect(shineMaskContextRef, rect); CGContextSetFillColorWithColor(mainMaskContextRef, [UIColor whiteColor].CGColor); CGContextSetFillColorWithColor(shineMaskContextRef, [UIColor whiteColor].CGColor); // Create main mask shape CGContextMoveToPoint(mainMaskContextRef, 0, 0); CGContextAddEllipseInRect(mainMaskContextRef, imageRect); CGContextFillPath(mainMaskContextRef); // Create shine mask shape CGContextTranslateCTM(shineMaskContextRef, -(rect.size.width / 4), rect.size.height / 4 * 3); CGContextRotateCTM(shineMaskContextRef, -45.f); CGContextMoveToPoint(shineMaskContextRef, 0, 0); CGContextFillRect(shineMaskContextRef, CGRectMake(0, 0, rect.size.width / 8 * 5, rect.size.height)); CGImageRef mainMaskImageRef = CGBitmapContextCreateImage(mainMaskContextRef); CGImageRef shineMaskImageRef = CGBitmapContextCreateImage(shineMaskContextRef); CGContextRelease(mainMaskContextRef); CGContextRelease(shineMaskContextRef); // Done with mask context CGContextRef contextRef = UIGraphicsGetCurrentContext(); CGContextSaveGState(contextRef); CGImageRef imageRef = CGImageCreateWithMask(self.image.CGImage, mainMaskImageRef); CGContextTranslateCTM(contextRef, 0, rect.size.height); CGContextScaleCTM(contextRef, 1.0, -1.0); CGContextSaveGState(contextRef); // Draw image CGContextDrawImage(contextRef, rect, imageRef); CGContextRestoreGState(contextRef); CGContextSaveGState(contextRef); // Clip to shine's mask CGContextClipToMask(contextRef, self.bounds, mainMaskImageRef); CGContextClipToMask(contextRef, self.bounds, shineMaskImageRef); CGContextSetBlendMode(contextRef, kCGBlendModeLighten); CGContextDrawLinearGradient(contextRef, [self alphaGradient], CGPointMake(0, 0), CGPointMake(0, self.bounds.size.height), 0); CGImageRelease(mainMaskImageRef); CGImageRelease(shineMaskImageRef); CGImageRelease(imageRef); // Done with image CGContextRestoreGState(contextRef); CGContextSetLineWidth(contextRef, self.borderWidth); CGContextSetStrokeColorWithColor(contextRef, self.borderColor.CGColor); CGContextMoveToPoint(contextRef, 0, 0); CGContextAddEllipseInRect(contextRef, imageRect); // Drop shadow CGContextSetShadowWithColor(contextRef, self.shadowOffset, self.shadowBlur, self.shadowColor.CGColor); CGContextStrokePath(contextRef); CGContextRestoreGState(contextRef); }
示例图: