zoukankan      html  css  js  c++  java
  • IOS Quartz2D 通过UIColor生成图片

    普通生成

    示例代码:

    复制代码
    //这里实现普通生成图片的方法
    - (void)drawRect:(CGRect)rect {
    
        CGRect cxRect = CGRectMake(0, 0, 100, 100);
        
        UIGraphicsBeginImageContextWithOptions(cxRect.size, NO, 0);
        
        [[UIColor redColor] setFill];
        
        UIRectFill(cxRect);
        
        UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 100, 100)];
        
        imageView.image = image;
        
        [self addSubview:imageView];
    }
    复制代码

    效果图:

    渐变颜色生成

    示例代码:

    复制代码
    //这里实现渐变颜色生成图片的方法
    - (void)drawRect:(CGRect)rect {
    
        CGRect cxRect = CGRectMake(0, 0, 1, 1);
        UIGraphicsBeginImageContextWithOptions(cxRect.size, NO, 0);
        CGContextRef context = UIGraphicsGetCurrentContext();
        UIColor * beginColor = [UIColor greenColor];
        UIColor * endColor = [UIColor redColor];
        drawLinearGradient(context, cxRect, beginColor.CGColor, endColor.CGColor);
        CGContextRestoreGState(context);
        
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 100, 100)];
        
        imageView.image = image;
        
        [self addSubview:imageView];
    }
    
    void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor)
    {
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGFloat locations[] = { 0.0, 1.0 };
        
        NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor];
        
        CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
        CGPoint startPoint = CGPointMake(rect.size.width/2, 0);
        CGPoint endPoint = CGPointMake(rect.size.width/2, rect.size.height/1.5);
        
        CGContextSaveGState(context);
        CGContextAddRect(context, rect);
        CGContextClip(context);
        CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
        CGContextSetStrokeColorWithColor(context, [[UIColor clearColor] CGColor]);
    }
    复制代码

    效果图:

  • 相关阅读:
    实验一 命令解释程序
    复利计算器3.0更新版
    复利计算器3.0总结
    复利计算器2.0
    0414复利计算5.1-美观、输入更新
    0408结对汉堡-结对2.0
    0406复利计算程序5.0-结对编程
    《构建之法》第四章读后感
    复利计算4.0-单元测试
    操作系统实验一、 命令解释程序的编写
  • 原文地址:https://www.cnblogs.com/wuyuxin/p/7045626.html
Copyright © 2011-2022 走看看