zoukankan      html  css  js  c++  java
  • 设置UIImage 圆角

     //设置UIImage圆角

    @interface UIImage(UIRoundedRectImage)

    + (id) createRoundedRectImage:(UIImage*)image size:(CGSize)size;

    @end

    @implementation UIImage(UIRoundedRectImage)

    static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,

                                     float ovalHeight)

    {

        float fw,fh;

        if (ovalWidth == 0 || ovalHeight == 0) {

            CGContextAddRect(context, rect);

            return;

        }

        

        CGContextSaveGState(context);

        CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));

        CGContextScaleCTM(context, ovalWidth, ovalHeight);

        fw = CGRectGetWidth(rect) / ovalWidth;

        fh = CGRectGetHeight(rect) / ovalHeight;

        

        CGContextMoveToPoint(context, fw, fh/2);  // Start at lower right corner

        CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);  // Top right corner

        CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner

        CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner

        CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right

        

        CGContextClosePath(context);

        CGContextRestoreGState(context);

    }

    + (id) createRoundedRectImage:(UIImage*)image size:(CGSize)size

    {

        // the size of CGContextRef

        int w = size.width;

        int h = size.height;

        

        UIImage *img = image;

        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

        CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace,kCGImageAlphaPremultipliedFirst);

        CGRect rect = CGRectMake(0, 0, w, h);

        

        CGContextBeginPath(context);

        addRoundedRectToPath(context, rect, 5, 5);

        CGContextClosePath(context);

        CGContextClip(context);

        CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);

        CGImageRef imageMasked = CGBitmapContextCreateImage(context);

        CGContextRelease(context);

        CGColorSpaceRelease(colorSpace);

        return [UIImage imageWithCGImage:imageMasked];

    }

    @end

  • 相关阅读:
    【转】Android系统中Fastboot和Recovery所扮演的角色。
    【转】Android ROM分析(1):刷机原理及方法
    【转】ANDROIDROM制作(一)——ROM结构介绍、精简和内置、一般刷机过程
    【转】使用fastboot命令刷机流程详解
    检测是否安装或者开启flash
    CentOS中/英文环境切换教程(CentOS6.8)
    id: cannot find name for user ID xxx处理办法
    linux重命名所有find查找到的文件/文件夹
    linux过滤旧文件中的空行和注释行剩余内容组成新文件
    CentOS和AIX查看系统序列号
  • 原文地址:https://www.cnblogs.com/rollrock/p/5235793.html
Copyright © 2011-2022 走看看