颜色转图片、View转图片

1、颜色转图片
+ (UIImage *)createImageWithColor:(UIColor *)color {
   
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
2、View转图片(截屏)
+ (UIImage *)createImageWihtView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, view.layer.contentsScale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,024评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,268评论 4 61
  • 珍惜的道理每个人都懂,可就是这样的,未知的意外和遥远的死亡总会让人忽略真正重要的此刻。 好多人最后都是这样的。 天...
    白完阅读 477评论 0 1
  • D18。我不懂得西式早餐,也不懂得中式早餐,但我就喜欢瞎捣腾。在我连续捣腾十几个早晨后,我发现我爱上了这种捣腾,同...
    美食营养师黄琳艳阅读 238评论 0 0
  • 人类诞生是地球上最伟大事件。可是人类是怎么来的?我们是谁?我们要到哪儿去? 本世纪最大的科学发现之一,就是人类起源...
    量子哥阅读 1,649评论 5 12