ios 裁剪圆形头像_iOS裁剪出环形图片

ios 裁剪出一个圆形的头像,这样的例子在简书已经很多了,我就不详细的介绍了.今天我想讲的是怎么裁剪出一个环形的图片.废话不多说,见代码。

1:首先将一张图片裁剪成圆形图片,,/**圆形图片裁剪*/

- (UIImage *)wjf_circleImage

{

//利用self生成一张圆形图片

// 1.开启图形上下文

UIGraphicsBeginImageContextWithOptions(self.size,NO,0);

// 2.描述圆形路径

UIBezierPath*path = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(0,0,

self.size.width,self.size.height)];

// 3.设置裁剪区域

[pathaddClip];

// 4.画图

[selfdrawAtPoint:CGPointZero];

// 5.取出图片

UIImage*image =UIGraphicsGetImageFromCurrentImageContext();

// 6.关闭上下文

UIGraphicsEndImageContext();

returnimage;

}

2:用CGContextClearRect 的功能 制作环形图片- (UIImage*)getClearRectImage:(UIImage*)image{

UIGraphicsBeginImageContextWithOptions(image.size,NO,0.0f);

CGContextRefctx =UIGraphicsGetCurrentContext();

//默认绘制的内容尺寸和图片一样大,从某一点开始绘制

[imagedrawAtPoint:CGPointZero];

CGFloatbigRaduis = image.size.width/5;

CGRectcirleRect =CGRectMake(image.size.width/2-bigRaduis,

image.size.height/2-bigRaduis, bigRaduis*2, bigRaduis*2);

//CGContextAddArc(ctx,image.size.width/2-bigRaduis,image.size.height/2-bigRaduis,

bigRaduis, 0.0, 2*M_PI, 0);

CGContextAddEllipseInRect(ctx,cirleRect);

CGContextClip(ctx);

CGContextClearRect(ctx,cirleRect);

UIImage*newImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnnewImage;

}

如果你的图片是正方形的话,就大功告成了,但是你的图片是长方形呢,不用怕.

注:将长方形图片变成正方形图片:- (UIImage * ) getSquareImage: (UIImage

* ) image RangeCGRect: (CGRect) range

centerBool: (BOOL) centerBool {

/*如若centerBool为Yes则是由中心点取mCGRect范围的图片*/

floatimgWidth = image.size.width;

floatimgHeight = image.size.height;

floatviewWidth = range.size.width;

floatviewHidth = range.size.height;

CGRectrect;

if (centerBool)

rect = CGRectMake((imgWidth - viewWidth) / 2,

(imgHeight - viewHidth) / 2, viewWidth, viewHidth);

else {

if (viewHidth

{

if (imgWidth <= imgHeight) {

rect = CGRectMake(0, 0, imgWidth, imgWidth * viewHidth / viewWidth);

} else {

floatwidth = viewWidth * imgHeight / viewHidth;

floatx = (imgWidth - width) / 2;

if (x > 0) {

rect = CGRectMake(x, 0, width, imgHeight);

} else {

rect = CGRectMake(0, 0, imgWidth, imgWidth * viewHidth / viewWidth);

}

}

} else {

if (imgWidth <= imgHeight) {

floatheight = viewHidth * imgWidth / viewWidth;

if (height 

rect = CGRectMake(0, 0, imgWidth, height);

} else

{

rect = CGRectMake(0, 0, viewWidth * imgHeight / viewHidth, imgHeight);

}

} else

{

floatwidth = viewWidth * imgHeight / viewHidth;

if (width 

{

floatx = (imgWidth - width) / 2;

rect = CGRectMake(x, 0, width, imgHeight);

} else

{

rect = CGRectMake(0, 0, imgWidth, imgHeight);

}

}

}

}

CGImageRefSquareImageRef =

CGImageCreateWithImageInRect(image.CGImage, rect);

CGRectSquareImageBounds = CGRectMake(0, 0, CGImageGetWidth(SquareImageRef),

CGImageGetHeight(SquareImageRef));

UIGraphicsBeginImageContext(SquareImageBounds.size);

CGContextRefcontext = UIGraphicsGetCurrentContext();

CGContextDrawImage(context, SquareImageBounds, SquareImageRef);

UIImage * SquareImage = [UIImageimageWithCGImage: SquareImageRef];

UIGraphicsEndImageContext();

returnSquareImage;

}

当然这是就用到了UIimage的size的属性了.CGSize size = yuanlai.size;

float imageSize;

NSLog(@"size==height%f====width%f",size.height,size.width);

if(size.height>= size.width) {

imageSize = size.width;

}else{

imageSize = size.height;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值