zoukankan      html  css  js  c++  java
  • 裁剪出环形图片

    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;
    }
  • 相关阅读:
    The Future of Middleware and the BizTalk Roadmap
    FW: How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
    Strips illegal Xml characters
    luogu P2280 激光炸弹(二维前缀和)
    luogu P2704 炮兵阵地(经典状态压缩DP)
    SP1716 GSS3 Can you answer these queries III (线段树维护最大连续子段和)
    二分图判定、匹配问题
    C++语法综合 | 基于char*设计一个字符串类MyString
    luogu P1044 火车进出栈问题(Catalan数)
    C++设计模式 | 三种设计模式基础
  • 原文地址:https://www.cnblogs.com/isItOk/p/5866876.html
Copyright © 2011-2022 走看看