美文网首页
针对不用layer的切图处理

针对不用layer的切图处理

作者: 一米押金 | 来源:发表于2016-12-07 10:17 被阅读0次

如果要适配ios7的系统,要给一张图片切成一张圆形图,用layer的圆角发现程序很"卡",所以如果可以,用“绘图”技术来解决问题,通过看视频,获取了信息,封装了一个工具分类

.h文件

/*  设置圆环切图

*  name:要切图的图片名

*  borderWidth: 被切掉的圆环的边框宽度

*  borderColor: 边框颜色

*/

+(instancetype)circleImageWithName:(NSString *)name borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor;

/**

*  圆形图片

*/

-(UIImage *)circleImage;

.m文件

+(instancetype)circleImageWithName:(NSString *)name borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor

{

//获得老图片

UIImage *oldimage = [UIImage imageNamed:name];

//开始画图上下文(由于外面要设计一个大圆环,所以这个小相对大一点了)

CGFloat imgW = oldimage.size.width + borderWidth;

CGFloat imgH = oldimage.size.height + borderWidth;

CGSize imgSize = CGSizeMake(imgW, imgH);

UIGraphicsBeginImageContextWithOptions(imgSize, NO, 0.0);

//获得上下文

CGContextRef ref = UIGraphicsGetCurrentContext();

//画外面的圆环

[borderColor set];

CGFloat bigRadius = imgW * 0.5;

CGFloat currentX = bigRadius;

CGFloat currentY = bigRadius;

CGContextAddArc(ref, currentX, currentY, bigRadius, 0, M_PI * 2, 0);

CGContextFillPath(ref);

//画里面的小圆

CGFloat smallRadius = bigRadius - borderWidth;

CGContextAddArc(ref, currentX, currentY, smallRadius, 0, M_PI * 2, 0);

//裁剪

CGContextClip(ref);

//画图

[oldimage drawInRect:CGRectMake(borderWidth, borderWidth, oldimage.size.width, oldimage.size.height)];

//取图

UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();

//结束上下文

UIGraphicsEndImageContext();

return newImg;

}

-(UIImage *)circleImage

{

//第二个参数填NO是透明

UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);

//获得上下文

CGContextRef ctx = UIGraphicsGetCurrentContext();

//获得一个圆

CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);

CGContextAddEllipseInRect(ctx, rect);

//裁剪

CGContextClip(ctx);

//将图片画上去

[self drawInRect:rect];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;

}

相关文章

  • 针对不用layer的切图处理

    如果要适配ios7的系统,要给一张图片切成一张圆形图,用layer的圆角发现程序很"卡",所以如果可以,用“绘图”...

  • Layer处理

    UIView设置边框阴影时,必须设置一个背景颜色,不然不出来。 UITableView 设置边框阴影 UITabl...

  • iOS 处理图片切圆加阴影

    imageview或者UIView等控件正常切圆处理 绘制阴影 添加到layer层中所需位置 效果图

  • iPhone屏幕圆角continuous corner

    1.通过设置layer的cornerRaidus实现圆角(图1 2.设置layer的cornerCurve(图2c...

  • js+html+css制作弹窗

    效果图: 准备: 引入layer.js文件,下载地址:layer.layui.com/下载后解压并把layer文件...

  • IOS anchorPoint和postion的关系

    首先注意一点1)anchorPoint是针对本身的layer的2)position是相对于父layer的 所以如果...

  • Nervos,一个有独特分层思想和通证体系的公链项目

    目前有很多的项目针对区块链的性能扩展方案,无论是Layer 1 (on-chain,基于链上)还是Layer 2(...

  • icon

    1.切图psd-选中图层-在图层中右击,选择Duplicate layer复制图层,new-image -> Tr...

  • iconfont方法总结

    切图 选择psd格式的图片,图标右键选择对应图层,在对应图层上右键选择Duplicate Layer复制图层保存在...

  • 27-高性能圆角处理

    第一种高性能圆角处理的方法 使用的视图的layer.cornerRadius和layer.masksToBound...

网友评论

      本文标题:针对不用layer的切图处理

      本文链接:https://www.haomeiwen.com/subject/zksdmttx.html