zoukankan      html  css  js  c++  java
  • iOS 开发切割圆角图片

    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, 10, 10);
        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];
    }
  • 相关阅读:
    C#模拟POST表单提交 WebClient
    视频广告屏蔽器(附下载地址)
    SQL Server 不同数据库导入指定数据解决方案
    WinRAR(WinZip)压缩与解压实现(C#版Window平台)
    Visual Studio 扩展包(.vsix)制作
    ORM for Net主流框架汇总与效率测试
    文件删除小助手
    C# 控制台应用程序输出颜色字体[更正版]
    IE与IE内核浏览器的那点事
    where in的sql语句按照指定ID进行排序的解决方法
  • 原文地址:https://www.cnblogs.com/fs-ios/p/5220993.html
Copyright © 2011-2022 走看看