ios 圆角生成

- (void)addImageWithCornerRadius:(CGFloat)radius color:(UIColor *)color;

- (void)addImageWithCornerRadius:(CGFloat)radius color:(UIColor *)color size:(CGSize)size;


@implementation UIColor(Components)

- (NSString *)colorComponent {

CGColorRef cgColor = self.CGColor;

const CGFloat *colorComponents = CGColorGetComponents(cgColor);

CGFloat r = colorComponents[0];

CGFloat g = colorComponents[1];

CGFloat b = colorComponents[2];

NSString *result = [NSString stringWithFormat:@"r%f_g%f_b%f", r, g, b];

return result;

}

@end

@implementation UIView(CornerRadius)

- (void)addImageWithCornerRadius:(CGFloat)radius color:(UIColor *)color {

__weak __typeof(self) weakSelf = self;

dispatch_async(dispatch_get_main_queue(), ^{

[weakSelf addImageWithCornerRadius:radius color:color size:weakSelf.bounds.size];

});

}

- (void)addImageWithCornerRadius:(CGFloat)radius color:(UIColor *)color size:(CGSize)size {

// 根据颜色,圆角程度,尺寸 命名文件

NSString *name = [NSString stringWithFormat:@"%@_%f_%@.png", [color colorComponent], radius, NSStringFromCGSize(size)];

NSString *fullPath = [[self pathWithFolder:@"CornerRadius"] stringByAppendingPathComponent:name];

// 判断本地是否已经有缓存了

NSFileManager *fileManager = [NSFileManager defaultManager];

BOOL isExists = [fileManager fileExistsAtPath:fullPath];

UIImage *image;

if (isExists) {

// 从缓存中获取

image = [UIImage imageNamed:fullPath];

} else {

// 缓存中没有 -> 生成图片 -> 保存

image = [self getImageWithSize:size color:color radius:radius];

NSData *data = UIImagePNGRepresentation(image);

[data writeToFile:fullPath atomically:YES];

}

// 讲生成的图片覆盖到当前的图层上

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

imageView.frame = CGRectMake(0, 0, size.width, size.height);

[self addSubview:imageView];

}

- (UIImage *)getImageWithSize:(CGSize)size color:(UIColor *)color radius:(CGFloat)radius {

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);

UIBezierPath *aPath = [UIBezierPath bezierPathWithRect:CGRectMake(0,0,size.width,size.height)];

UIBezierPath *bPath = [UIBezierPath bezierPathWithRoundedRect:aPath.bounds cornerRadius:radius];

[aPath appendPath:[bPath bezierPathByReversingPath]];

[color setFill];

[aPath fill];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;

}

- (NSString *)pathWithFolder:(NSString *)folder {

NSString *documentPath = [self documentPath];

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *folderPath = [documentPath stringByAppendingPathComponent:folder];

BOOL isExist = [fileManager fileExistsAtPath:folderPath];

if (!isExist) {

[fileManager createDirectoryAtPath:folderPath

withIntermediateDirectories:YES

attributes:nil

error:nil];

}

return folderPath;

}

- (NSString *)documentPath {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

NSString *path = [paths objectAtIndex:0];

return path;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • #define kBlackColor [UIColor blackColor] //.h //划线 + (voi...
    CHADHEA阅读 4,198评论 0 1
  • 1、改变 UITextField 占位文字 颜色和去掉底部白框 [_userName setValue:[UICo...
    i_MT阅读 4,688评论 0 2
  • 在这里总结一些iOS开发中的小技巧,能大大方便我们的开发 原文地址:http://www.jianshu.com/...
    Marray阅读 2,688评论 0 0
  • ### 生成嵌入logo的二维码 -(void)buildAppCIImageWithinImageView:(U...
    天使君阅读 3,624评论 0 1
  • 不知道这几天是怎么过的,到此刻元气才稍微恢复了一点,静静的坐在桌前一句话也不想说,理一理这生活却不知道该如...
    林雅阅读 3,851评论 7 4