裁剪出环形图片

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

posted on 2016-09-12 23:44  &#127774;Bob  阅读(540)  评论(0编辑  收藏  举报

导航