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;
}

源码

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • Angular 图片懒加载

    Angular图片懒加载有很多写好的nglibrary可以用,我这里用的是ng-lazyload-image。可以在npm官网直接搜索。安装npminstallng-lazyload-image–save在module中导入:import{CommonModule}from’@angular/common’;import{LazyLoad…

  • SSM框架原理,作用及使用方法

    SSM框架原理,作用及使用方法作用:SSM框架是springMVC,spring和mybatis框架的整合,是标准的MVC模式,将整个系统划分为表现层,controller层,service层,DAO层四层使用springMVC负责请求的转发和视图管理spring实现业务对象管理,mybatis作为数据对象的持久化引擎原理:SpringMVC:1.客户端发送请求到DispacherServ

  • Symantec赛门铁克安全软件免密卸载方式[通俗易懂]

    Symantec赛门铁克安全软件免密卸载方式[通俗易懂]装了Symantec后,后面希望卸载他,结果发现卸载需要卸载口令,查了一堆资料,总结有如下几种:1、卸载口令可能是symantec,反正没成本可以简单试试看。不过我是没有通过,这个口令不对我的Symantec。2、使用cleanwipe进行卸载,这是官方的用于卸载Symantec软件的工具。工具很小,应该有版本要求,旧版的不能完成卸载。推荐使用这个方式。我用的是CleanWipe_14.3.558.1000,选中下图中框出来的三个勾,直接下一步即可完成卸载。链接:https://pan.baidu.

  • ATM(异步传输模式)是什么?

    ATM(异步传输模式)是什么?异步传输模式(ATM)也称为信元中继(在固定大小的信元中传输数据),通过光纤或双绞线电缆(高速交换)在OSI模型的数据链路层(第2层)运行基于ITU-T宽带综合业务数字网络(B-ISDN)标准的网络技术,该标准是电信业开发的自动取款机可以同时传输各种流量:语音、视频和数据,速度高达每秒155兆比特。它将语音和视频数据转换成数据包,并通过相同的介质传输大数据包数据。自动取款机和TCP。由于两个端点之间使用固定通道路由协议路由,所以/IP是不同的。实时低延迟应用程序,如VoIP和视频,在ATM网络上..

  • Object-c @property的用法

    Object-c @property的用法

  • 全排列递归算法_全排列递归算法

    全排列递归算法_全排列递归算法一.全排列算法首先:什么是全排列=》百度一下从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取出m个元素的一个排列。当m=n时所有的排列情况叫全排列。公式:全排列

发表回复

您的电子邮箱地址不会被公开。

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