图片切圆处理
2016-07-19 16:41
435 查看
新建UIImage的分类声明发方法并实现
+(UIImage *)imageWithName:(NSString *)imageName imageBorder:(CGFloat)border borderColor:(UIColor
*)color{
// 设置生成切圆的 外圆环的宽度
CGFloat circleBorder = border;
UIImage *oldImage = [UIImageimageNamed:imageName];
// 新图片的尺寸
CGFloat newImageWidth = oldImage.size.width+2*circleBorder;
CGFloat newImageHeight = oldImage.size.height+2*circleBorder;
CGFloat circleW = (newImageWidth > newImageHeight) ? newImageHeight:newImageWidth;
// 开启上下文
UIGraphicsBeginImageContextWithOptions(CGSizeMake(circleW, circleW),NO,
0.0);
// 画大圆
UIBezierPath *pathBigCircle = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(0,0,
circleW, circleW)];
// 获取当前的上下文
CGContextRef contextRef =UIGraphicsGetCurrentContext();
// 添加到上下文
CGContextAddPath(contextRef, pathBigCircle.CGPath);
[color set];
// 渲染
CGContextFillPath(contextRef);
CGRect clip =CGRectMake(circleBorder, circleBorder, oldImage.size.width,
oldImage.size.height);
// 正切与OldImage的圆
UIBezierPath *pathSmallCircle = [UIBezierPathbezierPathWithOvalInRect:clip];
// 设置裁剪区域
[pathSmallCircle addClip];
// 画图片
[oldImage drawAtPoint:CGPointMake(circleBorder, circleBorder)];
// 生成新的图片
UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
+(UIImage *)imageWithName:(NSString *)imageName imageBorder:(CGFloat)border borderColor:(UIColor
*)color{
// 设置生成切圆的 外圆环的宽度
CGFloat circleBorder = border;
UIImage *oldImage = [UIImageimageNamed:imageName];
// 新图片的尺寸
CGFloat newImageWidth = oldImage.size.width+2*circleBorder;
CGFloat newImageHeight = oldImage.size.height+2*circleBorder;
CGFloat circleW = (newImageWidth > newImageHeight) ? newImageHeight:newImageWidth;
// 开启上下文
UIGraphicsBeginImageContextWithOptions(CGSizeMake(circleW, circleW),NO,
0.0);
// 画大圆
UIBezierPath *pathBigCircle = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(0,0,
circleW, circleW)];
// 获取当前的上下文
CGContextRef contextRef =UIGraphicsGetCurrentContext();
// 添加到上下文
CGContextAddPath(contextRef, pathBigCircle.CGPath);
[color set];
// 渲染
CGContextFillPath(contextRef);
CGRect clip =CGRectMake(circleBorder, circleBorder, oldImage.size.width,
oldImage.size.height);
// 正切与OldImage的圆
UIBezierPath *pathSmallCircle = [UIBezierPathbezierPathWithOvalInRect:clip];
// 设置裁剪区域
[pathSmallCircle addClip];
// 画图片
[oldImage drawAtPoint:CGPointMake(circleBorder, circleBorder)];
// 生成新的图片
UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 按右键另存图片只能存BMP
- photoshop去除图片上的水印
- upload上传单张图片
- 图片引发的溢出危机(图)
- C# WinForm控件对透明图片重叠时出现图片不透明的简单解决方法
- C#实现把彩色图片灰度化代码分享
- C#将图片和字节流互相转换并显示到页面上
- C#监控文件夹并自动给图片文件打水印的方法
- 纯CSS实现的当鼠标移上图片添加阴影效果代码
- 如何使用C#从word文档中提取图片
- C#实现打开画图的同时载入图片、最大化显示画图窗体的方法
- C#图片添加水印的实现代码
- 随鼠标移动的图片或文字特效代码