用颜色直接生成image

+ (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();
    }];

你可能感兴趣的:(用颜色直接生成image)