iOS 不规则的ImageView「建议收藏」

iOS 不规则的ImageView

大家好,又见面了,我是全栈君。

我们在做iOS开发的时候,往往须要实现不规则形状的头像,如:

iOS 不规则的ImageView「建议收藏」

那怎样去实现?

通常图片都是矩形的,假设想在client去实现不规则的头像,须要自己去实现。

1.使用layer去实现, 见http://blog.csdn.net/johnzhjfly/article/details/39993345

2.使用CAShapeLayer, CALayer怎样去实现

我们来看看怎样使用CAShapeLayer去实现,

定义一个ShapedImageView。继承于UIView, 代码例如以下:

#import "ShapedImageView.h"

@interface ShapedImageView()
{
    CALayer      *_contentLayer;
    CAShapeLayer *_maskLayer;
}
@end

@implementation ShapedImageView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup
{
    _maskLayer = [CAShapeLayer layer];
    _maskLayer.path = [UIBezierPath bezierPathWithOvalInRect:self.bounds].CGPath;
    _maskLayer.fillColor = [UIColor blackColor].CGColor;
    _maskLayer.strokeColor = [UIColor redColor].CGColor;
    _maskLayer.frame = self.bounds;
    _maskLayer.contentsCenter = CGRectMake(0.5, 0.5, 0.1, 0.1);
    _maskLayer.contentsScale = [UIScreen mainScreen].scale;
    
    _contentLayer = [CALayer layer];
    _contentLayer.mask = _maskLayer;
    _contentLayer.frame = self.bounds;
    [self.layer addSublayer:_contentLayer];
    
}

- (void)setImage:(UIImage *)image
{
    _contentLayer.contents = (id)image.CGImage;
}

@end

声明了用于maskLayer个CAShapedLayer。 CAShapedLayer有个path的属性。将内容Layer的mask设置为maskLayer, 就能够获取到我们想要的形状。

path我们能够使用CAMutablePath随意的构造,上述的代码执行想过例如以下:

iOS 不规则的ImageView「建议收藏」

假设将代码改成

    _maskLayer = [CAShapeLayer layer];
    _maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:20].CGPath;
    _maskLayer.fillColor = [UIColor blackColor].CGColor;
    _maskLayer.strokeColor = [UIColor redColor].CGColor;
    _maskLayer.frame = self.bounds;
    _maskLayer.contentsCenter = CGRectMake(0.5, 0.5, 0.1, 0.1);
    _maskLayer.contentsScale = [UIScreen mainScreen].scale;                 //很关键设置自己主动拉伸的效果且不变形
    
    _contentLayer = [CALayer layer];
    _contentLayer.mask = _maskLayer;
    _contentLayer.frame = self.bounds;
    [self.layer addSublayer:_contentLayer];

的效果:

iOS 不规则的ImageView「建议收藏」

假设将代码改成:

    CGMutablePathRef path = CGPathCreateMutable();
    CGPoint origin = self.bounds.origin;
    CGFloat radius = CGRectGetWidth(self.bounds) / 2;
    CGPathMoveToPoint(path, NULL, origin.x, origin.y + 2 *radius);
    CGPathMoveToPoint(path, NULL, origin.x, origin.y + radius);
    
    CGPathAddArcToPoint(path, NULL, origin.x, origin.y, origin.x + radius, origin.y, radius);
    CGPathAddArcToPoint(path, NULL, origin.x + 2 * radius, origin.y, origin.x + 2 * radius, origin.y + radius, radius);
    CGPathAddArcToPoint(path, NULL, origin.x + 2 * radius, origin.y + 2 * radius, origin.x + radius, origin.y + 2  * radius, radius);
    CGPathAddLineToPoint(path, NULL, origin.x, origin.y + 2 * radius);
    
    _maskLayer = [CAShapeLayer layer];
    _maskLayer.path = path;
    _maskLayer.fillColor = [UIColor blackColor].CGColor;
    _maskLayer.strokeColor = [UIColor clearColor].CGColor;
    _maskLayer.frame = self.bounds;
    _maskLayer.contentsCenter = CGRectMake(0.5, 0.5, 0.1, 0.1);
    _maskLayer.contentsScale = [UIScreen mainScreen].scale;                 //很关键设置自己主动拉伸的效果且不变形
    
    _contentLayer = [CALayer layer];
    _contentLayer.mask = _maskLayer;
    _contentLayer.frame = self.bounds;
    [self.layer addSublayer:_contentLayer];

将是这个效果:

iOS 不规则的ImageView「建议收藏」

理论上我们能够构造出随意想要的形状。可是有些形状假设你不熟悉几何知识的话是构造不出正确的

path的,从代码上我们能够看到我们能够通过设置CALayer的contents属性来设置显示的内容,那我们

是不是能够通过设置CAShapedLayer的contents来设置maskLayer呢?答案是肯定的,代码例如以下:

    _maskLayer = [CAShapeLayer layer];
    _maskLayer.fillColor = [UIColor blackColor].CGColor;
    _maskLayer.strokeColor = [UIColor clearColor].CGColor;
    _maskLayer.frame = self.bounds;
    _maskLayer.contentsCenter = CGRectMake(0.5, 0.5, 0.1, 0.1);
    _maskLayer.contentsScale = [UIScreen mainScreen].scale;                 //很关键设置自己主动拉伸的效果且不变形
    _maskLayer.contents = (id)[UIImage imageNamed:@"gray_bubble_right@2x.png"].CGImage;
    
    _contentLayer = [CALayer layer];
    _contentLayer.mask = _maskLayer;
    _contentLayer.frame = self.bounds;
    [self.layer addSublayer:_contentLayer];

gray_bubble_right就是你想要的形状,执行效果例如以下:

iOS 不规则的ImageView「建议收藏」

不停的改变CALayer的一个坏处就是很的损耗性能,假设你有一个cell的列表。每一个列表有个头像的话。高速滑动的时候。你会发现很的卡。

此时理想的解决方式是使用CGPath或者UIBezierPath构建不规则的path,然后clip画出来。这里就不具体解说了。

演示样例代码例如以下:

- (UIImage *)maskImage
{
    // start with an image
    UIImage * fooImage = self;//[UIImage imageNamed:@"foo.png"];
    CGRect imageRect = CGRectMake(0, 0, fooImage.size.width, fooImage.size.height);
    // set the implicit graphics context ("canvas") to a bitmap context for images
    UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0.0);
    // create a bezier path defining rounded corners
    UIBezierPath * path = [UIBezierPath bezierPathWithRect:imageRect];
    CGFloat radius = fooImage.size.width / 2.5;
    CGFloat _radius = radius;
    //construct your shaped path
    [path moveToPoint:CGPointMake(0, 0)];
    [path addArcWithCenter:CGPointMake(radius, radius) radius:_radius startAngle:M_PI endAngle:3 * M_PI / 2 clockwise:TRUE];
    [path moveToPoint:CGPointMake(fooImage.size.width, 0)];
    [path addArcWithCenter:CGPointMake(fooImage.size.width - radius, radius) radius:_radius startAngle:3 * M_PI / 2 endAngle:2 * M_PI clockwise:TRUE];
    [path moveToPoint:CGPointMake(fooImage.size.width, fooImage.size.height)];
    [path addArcWithCenter:CGPointMake(fooImage.size.width - radius, fooImage.size.height - radius) radius:_radius startAngle:0 endAngle:M_PI / 2 clockwise:TRUE];
    [path moveToPoint:CGPointMake(0, fooImage.size.height)];
    [path addArcWithCenter:CGPointMake(radius, fooImage.size.height - radius) radius:_radius startAngle:M_PI / 2 endAngle:M_PI clockwise:TRUE];
    path.flatness = 1000;
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    // use this path for clipping in the implicit context
    [path addClip];
    // draw the image into the implicit context
    [fooImage drawInRect:imageRect];
    // save the clipped image from the implicit context into an image
    UIImage *maskedImage = UIGraphicsGetImageFromCurrentImageContext();
    // cleanup
    UIGraphicsEndImageContext();
    return maskedImage;
}

源码

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115792.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java俄罗斯方块游戏代码建议收藏

    java俄罗斯方块游戏代码:1packagecom;23importjava.awt.Color;4importjava.awt.Graphics;5importjava.a

    2021年12月20日
    61
  • linux安装pycharm报错:Unable to detect graphics environment[通俗易懂]

    linux安装pycharm报错:Unable to detect graphics environment[通俗易懂]执行shpycharm.sh时,报错:StartupErrorUnabletodetectgraphicsenvironment解决方法:重新开启一个终端,以用户而非root身份登入,重新执行shpycharm.sh很神奇,但是成功了!

    2022年10月10日
    3
  • HTML表格代码_html如何制作表格代码

    HTML表格代码_html如何制作表格代码表格代码<table></tabie><tablewidth(表格宽度。可以用像素或百分比表示。)=””height=””(行高)border=””(边框)cellpadding=””(内容跟单元格边框的边距。)cellspacing=””(单元格之间的间距。)align=””(对齐方式。)bgcolor=””(背景色)background=””(背景图片。)><tr(行)align=””(一行的内容的水平对齐方式)valign(一行的内容的垂平对齐.

    2022年8月11日
    9
  • RelativeLayout.LayoutParams

    RelativeLayout.LayoutParams通过id设置相对兄弟元素对齐。<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android&qu

    2022年7月1日
    25
  • Scrapy项目 – 数据简析 – 实现腾讯网站社会招聘信息爬取的爬虫设计

    Scrapy项目 – 数据简析 – 实现腾讯网站社会招聘信息爬取的爬虫设计一、数据分析截图本例实验,使用Weka3.7对腾讯招聘官网中网页上所罗列的招聘信息,如:其中的职位名称、链接、职位类别、人数、地点和发布时间等信息进行数据分析,详见如下图:图1-1Weka3.7分析界面图1-2职位数据ZeroR分析界面图1-3数据聚类分析界面图1-4数据Visualize分析界面二、数据分析结论由图2-1可知,随着应聘人数的…

    2022年5月2日
    55
  • 最受欢迎的8个Python框架,满足你的各类需求「建议收藏」

    最受欢迎的8个Python框架,满足你的各类需求「建议收藏」今天给大家分享几个最受欢迎的Python框架。这些框架包括Web开发,高性能网络通信,测试,爬虫等等,如果你正在学习Python,那么应该可以满足你。1DjangoDjango应该是最出名的Python框架,是一款在数据库功能、后台功能、模板系统、网址匹配、缓存系统等方面有“先天”优势的开源框架。它可以通过几行简单的代码就让你的网站拥有一个强大的后台,轻松管理你的内容;强大,易扩展的模板系统,设计简易,代码,样式分开设计,更容易管理。小编整理的一整套系统的p-ython学习教程从最基础的

    2022年5月10日
    43

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号