用颜色直接生成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();
    }];
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 47,338评论 22赞 665
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 180,678评论 25赞 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 16,922评论 4赞 61
  • 亲爱的禾谷, 你好!今天,妈妈有了一个小小的幸福行动计划,给你写情书!哈,表达最真的爱和欣赏是妈妈的心愿,希望它能...
    草马束木阅读 277评论 2赞 1
  • 我们可不可以不忧伤 那一刻,我觉得我是世界上最幸福的人。就像牛奶与咖啡,当咖啡的苦涩遇上牛奶的香甜,碰撞出我...
    染景紫阅读 547评论 0赞 2

友情链接更多精彩内容