欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用颜色直接生成image

程序员文章站 2022-04-09 21:27:37
...
+ (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

附送裁剪圆形图片的方法

 [self.companyImg sd_setImageWithURL:[NSURL URLWithString:model.logo] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

        //操作图像的设置
        //UIImage提供了如何绘制自身的方法
        UIGraphicsBeginImageContextWithOptions(self.companyImg.bounds.size, NO, [UIScreen mainScreen].scale);
        [[UIBezierPath bezierPathWithRoundedRect:self.companyImg.bounds cornerRadius:self.companyImg.bounds.size.width/2] addClip];
        [image drawInRect:self.companyImg.bounds];
        self.companyImg.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }];