裁剪出环形图片

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 < imgHeight) {

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

                } else

                {

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

                }

            } else

            {

                floatwidth = viewWidth * imgHeight / viewHidth;

                if (width < imgWidth)

                {

                    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;
}
时间: 2024-08-06 03:47:03

裁剪出环形图片的相关文章

一招教你轻松从图像中裁剪出婚纱礼服和面纱

Super PhotoCut Pro是一款简单粗暴的抠图软件,它能够准确地覆盖你想要去除的任何一个细节区域,抠出图片中你最想要的部分,不需要任何的技巧,那么photocut怎么用呢?你只需要在图片上勾画出需要保留的部分,然后程序会自动处理.从透明物体中删除图像背景是一项复杂而困难的消光任务,即使在您精通Photoshop的前提下也是如此,但现在super photocut将帮助您有效地完成任务, 一分钟抠图不是神话! 使用Super PhotoCut Pro从图像中裁剪出婚纱礼服和面纱教程 1.

jQuery实现的鼠标悬浮链接弹出跟随图片代码

jQuery实现的鼠标悬浮链接弹出跟随图片代码:本章节介绍一下一种比较常用的效果,那就是当鼠标滑过链接的时候,能够出现跟随鼠标指针移动的图层,在实际应用中,一般是对于链接的一些说明文字或者图片等等,下面是代码实例: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.so

iOS圆形图片裁剪,原型图片外面加一个圆环

/** *  在圆形外面加一个圆环 */ - (void)yuanHuan{ //0.加载图片 UIImage *image = [UIImage imageNamed:@"AppIcon1024"]; //图片的宽度 CGFloat imageWH = image.size.width; //设置圆环的宽度 CGFloat border = 1; //大圆形的宽度高度 CGFloat ovalWH = imageWH + 2 * border; //1.开启位图上下文 UIGraph

基于jQuery向下弹出遮罩图片相册

今天给大家分享一款基于jQuery向下弹出遮罩图片相册.单击相册图片时,一个遮罩层从上到下动画出现.然后弹出显示图片.这款插件适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗. 在线预览   源码下载 实现的代码. html代码: <center> <h1> 点击图片查看效果</h1> </center> <br> <br> <div id='container'>

基于jQuery弹出层图片动画查看代码

分享一款基于jQuery弹出层图片动画查看代码是一款鼠标单击文字或图片内容放大显示且含圆角投影效果.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class="zxx_out_box"> <div class="zxx_in_box"> <h3 class="zxx_title"> jQuery-单击文字或图片内容放大显示且含圆角投影效果</h3> <div c

css制作的弹出式图片

<html> <head> <title>css制作的弹出式图片预览效果|梨花粉价格|石家庄水泵厂</title> <style type="text/css"> .thumbnail{ position: relative; z-index: 0; } .thumbnail:hover{ background-color: transparent; z-index: 50; } .thumbnail span{ /*CSS

使用jquery完成定时弹出广告图片

<script src="../js/jquery-1.8.3.js"></script> <script type="text/javascript"> $(function(){ //1.书写显示广告的定时操作 time=setInterval("showAdd()",3000); }); //2.书写显示广告图片的函数 function showAdd(){ //3.获取广告图片,并让其显示 // $(&

使用JCrop进行图片裁剪,裁剪js说明,裁剪预览,裁剪上传,裁剪设计的图片处理的工具类和代码

?? 1.要想制作图片裁剪功能,可以使用网上的裁剪工具JCrop,网址是:https://github.com/tapmodo/Jcrop/ 案例效果如下: 2.引入JCrop的js代码,具体要引入那些js可以参考JCrop案例: 3.编写的html代码如下: <div id="light" class="white_content"> <div class="vatitlee"> 封面截取 <div class=&

React+Antd+Antd-Img-Crop实现上传固定大小的裁剪头像或者图片(且可控制图片数量)

见章知著 1024,程序员们节日快乐!本文主要讲述react配合antd以及antd-img-crop第三方库实现一个可控的图片上传功能. 运行项目 需要具有node环境 第三方库安装 1.antd(Upload)安装和初始化 1.1.在代码编辑器(VS-Code或者其他编辑器)代开终端,输入命令行安装antd: $ yarn add antd --save 或者 $ npm install antd --save 1.2.安装好antd后,修改src/App.css,在新建的项目中引入antd