zoukankan      html  css  js  c++  java
  • iOS开发-简单获取View截图图像(Quartz2D)

    1. 先指定图像的大小

    UIGraphicsBeginImageContext(view.frame.size);

    2. 在指定的区域绘制图像

    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];

    3. 获取图像上下文

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    4. 关闭图像上下文

    UIGraphicsEndImageContext();

    没错!只需要4行代码就可以获取到指定View的图像截图。

    另外,常用的绘制图像还有另一个方法:

    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    下面我补充一个方法例子:

    + (UIImage *)getImageViewWithView:(UIView *)view
    {
        UIGraphicsBeginImageContext(view.frame.size);
        [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return image;
    }

    博文作者:GarveyCalvin

    博文出处:http://www.cnblogs.com/GarveyCalvin/

    本文版权归作者和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作!

  • 相关阅读:
    柳下品茗
    游百花诗意亭有感
    爱情如酒
    笑傲IT文坛
    面对诱惑,你会屈从吗?
    拓展人脉的三十六计
    世界经理人推荐:拓展人脉的两大法宝
    我还能做什么
    诗缘
    有谁陪我风雨同舟
  • 原文地址:https://www.cnblogs.com/GarveyCalvin/p/4151637.html
Copyright © 2011-2022 走看看