UIImage

将一个View保存为PNG保存到本地

 

1.首先写一个UIImage的分类,加入这个方法。

+ (UIImage *)imageWithView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, view.layer.contentsScale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

 2.搞定!

[UIImagePNGRepresentation([UIImage imageWithView:containerView]) writeToFile:file atomically:YES];

 

UIImagePNGRepresentation()将UIImage转换为PNG格式。



UIColor转换图片
+ (UIImage *)imageWithColor:(UIColor *)color forSize:(CGSize)size {
    CGRect frameRect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(ctx, color.CGColor); //image frame color
    CGContextFillRect(ctx, frameRect);
    UIImage* resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultImage;
}

 

posted @ 2015-06-15 11:33  从良少年  阅读(183)  评论(0编辑  收藏  举报