美文网首页
iOS 画圆形图像的几种方法

iOS 画圆形图像的几种方法

作者: 绚雨蓝了个枫 | 来源:发表于2017-02-22 15:15 被阅读2555次

首先声明一个UIImageView

@property (nonatomic, strong) UIImageView *cycleImv;

方法一:

self.cycleImv= [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];

[self.view addSubview:self.cycleImv];

// 为图片切圆

self.cycleImv.layer.masksToBounds = YES;

self.cycleImv.layer.cornerRadius = self.cycleImv.frame.size.width / 2.0;

// 为图片添加边框,根据需要设置边框

self.cycleImv.layer.borderWidth = 2.0;//边框的宽度

self.cycleImv.layer.borderColor = [UIColor redColor].CGColor;//边框的颜色

方法二:

- (void)drawRect:(CGRect)rect

{

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.cycleImv.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:self.cycleImv.bounds.size];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];

//设置大小

maskLayer.frame = self.cycleImv.bounds;

//设置图形样子

maskLayer.path = maskPath.CGPath;

self.cycleImv.layer.mask = maskLayer;

}

方法三:

将网络图片裁剪为圆形,首先建立一个UIImage分类UIImage+Extension,一个UIImageView分类UIImageView+CircularImv。

UIImage+Extension.h文件

#import@interface UIImage (Extension)

- (UIImage *)circleImage;

@end

UIImage+Extension.m文件

#import "UIImage+Extension.h"

@implementation UIImage (Extension)

- (UIImage *)circleImage

{

// 开始图形上下文,NO代表透明

UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);

// 获得图形上下文

CGContextRef ctx = UIGraphicsGetCurrentContext();

// 设置一个范围

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

// 根据一个rect创建一个椭圆

CGContextAddEllipseInRect(ctx, rect);

// 裁剪

CGContextClip(ctx);

// 将原照片画到图形上下文

[self drawInRect:rect];

// 从上下文上获取剪裁后的照片

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

// 关闭上下文

UIGraphicsEndImageContext();

return newImage;

}

@end

使用了SDWebImage加载网络图片,所以加上UIImageView+WebCache.h头文件。

UIImageView+CircularImv.h文件

#import@interface UIImageView (CircularImv)

- (void)setCircularImvURL:(NSString *)imageUrl holderImageName:(NSString *)imageName;

@end

UIImageView+CircularImv.m文件

#import "UIImageView+CircularImv.h"

#import "UIImageView+WebCache.h"

#import "UIImage+Extension.h"

@implementation UIImageView (CircularImv)

- (void)setCircularImvURL:(NSString *)imageUrl holderImageName:(NSString *)imageName

{

//占位图片,当URL上下载的图片为空,就显示该图片

UIImage *placeholder = [[UIImage imageNamed:imageName] circleImage];

[self sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:placeholder completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

//当图片下载完会来到这个block,执行以下代码

self.image = image ? [image circleImage] : placeholder;

}];

}

@end

使用方法:

[self.cycleImv setCircularImvURL:网络图片地址 holderImageName:本地占位图片名称];

相关文章

  • iOS 画圆形图像的几种方法

    首先声明一个UIImageView @property (nonatomic, strong) UIImageVi...

  • 思维导图

    作业来了 尝试着用抽象词变为形象图像的几种常用转换方法把下列词语转化为图像。 我们看看哪位伙伴画的图像最有趣,最...

  • 4.7日作业 把词语转化为图像

    尝试着用抽象词变为形象图像的几种常用转换方法把下列词语转化为图像。 我们看看哪位伙伴画的图像最有趣,最能直观表达这...

  • 5月9号迭代

    尝试着用抽象词变为形象图像的几种常用转换方法把下列词语转化为图像。 我们看看哪位伙伴画的图像最有趣,最能直观表达这...

  • 5月9号思维导图作业

    尝试着用抽象词变为形象图像的几种常用转换方法把下列词语转化为图像。 我们看看哪位伙伴画的图像最有趣,最能直观表达这...

  • Swift iOS15 图片之 如何更改 UIImage 颜色

    SwiftUI Swift 新技能大全之 如何更改 UIImage 颜色 有几种方法可以在 iOS 中更改图像的颜...

  • iOS几种加密方式

    iOS 几种加密方法2017-06-19 [iOS开发] iOS常见的几种加密方法 普通加密方法是讲密码进行加密后...

  • iOS字体大小适配的几种方法

    iOS字体大小适配的几种方法 iOS字体大小适配的几种方法

  • iOS 开发中 runtime 常用的几种方法

    iOS 开发中 runtime 常用的几种方法 iOS 开发中 runtime 常用的几种方法

  • 边缘检测(上)

    前半部分的几种方法主要将一幅图像变换成只由边缘组成的图像,而后半部分的几种方法是将图像轮廓分离出来。当然这篇文章只...

网友评论

      本文标题:iOS 画圆形图像的几种方法

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