美文网首页iOS 开发 iOS Developer
手动创造纯色背景图

手动创造纯色背景图

作者: 云无心 | 来源:发表于2016-03-08 10:32 被阅读132次

    有个需求需要一个纯白色占位图用来在下载图片时如果网络不佳或者图片资源不存在使用,并不是很想麻烦UI给我切(其实是怕被砍),于是决定自己画一个.直接上代码

    - (UIImage *)ImageFromColor:(UIColor *)color{
    
      CGRect rect = CGRectMake(0, 0, 200, 200);
      UIGraphicsBeginImageContext(rect.size);
      CGContextRef context = UIGraphicsGetCurrentContext();
      CGContextSetFillColorWithColor(context, [color CGColor]);
      CGContextFillRect(context, rect);
      UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext(); 
      return img;
    }
    

    代码中的rect随便写的, 按需求啊各位,试试吧,还可以写个分类或者啥的使用更加方便

    相关文章

      网友评论

        本文标题:手动创造纯色背景图

        本文链接:https://www.haomeiwen.com/subject/tvlzkttx.html