zoukankan      html  css  js  c++  java
  • 绘制圆形发散渐变

    - (void)drawRadialGradient:(CGContextRef)context
      path:(CGPathRef)path
     startColor:(CGColorRef)startColor
      endColor:(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);
    
    
     CGRect pathRect = CGPathGetBoundingBox(path);
     CGPoint center = CGPointMake(CGRectGetMidX(pathRect), CGRectGetMidY(pathRect));
     CGFloat radius = MAX(pathRect.size.width / 2.0, pathRect.size.height / 2.0) * sqrt(2);
    
     CGContextSaveGState(context);
     CGContextAddPath(context, path);
     CGContextEOClip(context);
    
     CGContextDrawRadialGradient(context, gradient, center, 0, center, radius, 0);
    
     CGContextRestoreGState(context);
    
     CGGradientRelease(gradient);
     CGColorSpaceRelease(colorSpace);
    }
    
    - (void)viewDidLoad 
    {
     [super viewDidLoad];
     // Do any additional setup after loading the view.
    
     //创建CGContextRef
     UIGraphicsBeginImageContext(self.view.bounds.size);
     CGContextRef gc = UIGraphicsGetCurrentContext();
    
     //创建CGMutablePathRef
     CGMutablePathRef path = CGPathCreateMutable();
    
     //绘制Path
     CGRect rect = CGRectMake(0, 100, 300, 200);
     CGPathMoveToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect));
     CGPathAddLineToPoint(path, NULL, CGRectGetMidX(rect), CGRectGetMaxY(rect));
     CGPathAddLineToPoint(path, NULL, CGRectGetWidth(rect), CGRectGetMaxY(rect));
     CGPathAddLineToPoint(path, NULL, CGRectGetWidth(rect), CGRectGetMinY(rect));
     CGPathCloseSubpath(path);
    
     //绘制渐变
     [self drawRadialGradient:gc path:path startColor:[UIColor greenColor].CGColor endColor:[UIColor redColor].CGColor];
    
     //注意释放CGMutablePathRef
     CGPathRelease(path);
    
     //从Context中获取图像,并显示在界面上
     UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
    
     UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
     [self.view addSubview:imgView];
    }
    

     https://www.jb51.net/article/94356.htm

  • 相关阅读:
    iOS UIWebView中javascript与Objective-C交互、获取摄像头
    iOS UIWebView中javascript与Objective-C交互、获取摄像头
    android使用webview上传文件(支持相册和拍照)
    在Android浏览器中通过WebView调用相机拍照/选择文件 上传到服务器
    Android 访问权限设置
    perl 登入人人网
    攻击排查脚本
    利用套打和分栏巧妙来做商品价签
    perl 自定义请求头
    获取响应头信息
  • 原文地址:https://www.cnblogs.com/yuxiaoyiyou/p/13644745.html
Copyright © 2011-2022 走看看