美文网首页iOS开发
UIImageFromColor

UIImageFromColor

作者: 左方 | 来源:发表于2018-07-13 11:18 被阅读0次
@interface UIImage (color)

+ (UIImage *)createImageWithColor:(UIColor *)color;
+ (UIImage*)OriginImage:(UIImage *)image scaleToSize:(CGSize)size;
@end

@implementation UIImage (color)
+ (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 *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

+(UIImage*)OriginImage:(UIImage *)image scaleToSize:(CGSize)size
{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    
    // 绘制改变大小的图片
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    
    // 返回新的改变大小后的图片
    return scaledImage;
}
@end

相关文章

网友评论

    本文标题:UIImageFromColor

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