美文网首页
ios简单圆形图片裁剪

ios简单圆形图片裁剪

作者: 会写bug的程序媛 | 来源:发表于2018-11-29 14:42 被阅读0次

     //加载图片

        UIImage*image = [UIImageimageNamed:@"name"];

        //开启跟原始图片一样大的上下文

        UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);

        //2、设置一个圆形裁剪区域

        //2.1绘制一个圆形

        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];

        //2.2把圆形的路径设置在指定区域

        [pathaddClip];//超过裁剪区域以外的内容都给裁剪掉

        //3、把图片绘制到上下文当中

        [imagedrawAtPoint:CGPointZero];

        //4、从上下文当中取出图片

        UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();

        //5、关闭上下文

        UIGraphicsEndImageContext();

        //显示图片

        UIImageView*imageView= [[UIImageViewalloc]init];

        imageView.backgroundColor = [UIColor greenColor];

        imageView.frame=CGRectMake(100,300,100,100);

        imageView.image= newImage;

        [self.viewaddSubview:imageView];

    //绘制带边框的图片裁剪

    +(UIImage*)imageWithBorder:(CGFloat)borderW color:(UIColor*)color name:(UIImage*)image{

        //0、加载图片

        //1、确定边框宽度

        CGFloatborderWidth = borderW;

        //2、开启一个上下文

        CGSizesize =CGSizeMake(image.size.width+2*borderWidth, image.size.height+2*borderWidth);

        UIGraphicsBeginImageContextWithOptions(size, NO, 0);

        //3、绘制大圆显示出来

        UIBezierPath*path = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(0,0, size.width, size.height)];

        [colorset];

        [pathfill];

        //4、绘制小圆,把小圆设置成裁剪区域

        UIBezierPath*smallPath =[UIBezierPathbezierPathWithOvalInRect:CGRectMake(borderWidth, borderWidth, image.size.width, image.size.height)];

        [smallPathaddClip];

        //5、把图片绘制到上下文当中

        [imagedrawAtPoint:CGPointMake(borderWidth, borderWidth)];

        //6、从上下文中取出图片

        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

        //7、关闭上下文

        UIGraphicsEndImageContext();

        returnnewImage;

    }

    相关文章

      网友评论

          本文标题:ios简单圆形图片裁剪

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