zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    2491 玉蟾宫
    1704 卡片游戏
    1020 孪生蜘蛛
    1215 迷宫
    3149 爱改名的小融 2
    1316 文化之旅 2012年NOIP全国联赛普及组
    1664 清凉冷水
    157. [USACO Nov07] 奶牛跨栏
    [SCOI2005]繁忙的都市
    【NOIP2014模拟赛No.1】我要的幸福
  • 原文地址:https://www.cnblogs.com/congliang/p/4576701.html
Copyright © 2011-2022 走看看