码迷,mamicode.com
首页 > 其他好文 > 详细

UIImage

时间:2015-06-15 12:59:52      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:

将一个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;
}

 

UIImage

标签:

原文地址:http://www.cnblogs.com/congliang/p/4576701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!