• iOS截屏


    - (UIImage *)capture
    {
        UIImage* image = nil;
    
        UIGraphicsBeginImageContext(TOP_VIEW.bounds.size);
        {
            [TOP_VIEW.layer renderInContext: UIGraphicsGetCurrentContext()];
            image = UIGraphicsGetImageFromCurrentImageContext();
        }
        UIGraphicsEndImageContext();
        
        if (image != nil) {
            return image;
        }
        return nil;
    }

    这里TOP_VIEW 是宏定义  

    [[UIApplication sharedApplication]keyWindow].rootViewController.view

        UIGraphicsBeginImageContext(TOP_VIEW.bounds.size);

    关于这个设置,api中有

    UIKIT_EXTERN void     UIGraphicsBeginImageContext(CGSize size);
    UIKIT_EXTERN void     UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) NS_AVAILABLE_IOS(4_0);

    以上两种设置,经过测试,UIGraphicsBeginImageContextWithOptions 在ios8下进app时会崩溃.用UIGraphicsBeginImageContext则没有这方面问题

    以上代码会造成不稳定因素...

    因此 附上另外一种实现方式

    创建UIView的category  

    实现以下方法 

     1 -(UIImage *)convertViewToImage
     2 
     3 {
     4     
     5     UIGraphicsBeginImageContext(self.bounds.size);
     6     
     7     [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
     8     
     9     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    10     
    11     UIGraphicsEndImageContext();
    12     
    13     return image;
    14     
    15 }

    然后截图的时候,操作当前view去执行  convertViewToImage即可获得截下来的image对象

  • 相关阅读:
    java第二次作业 数组和String类
    java第一次作业
    选择
    latex math
    sum的写法
    qt 4.8.5 vs 2012编译
    物联网笔记四:物联网网络及协议
    物联网学习笔记三:物联网网关协议比较:MQTT 和 Modbus
    物联网学习笔记二:物联网网关
    物联网学习笔记一:物联网入门的必备 7 大概念和技能
  • 原文地址:https://www.cnblogs.com/n1ckyxu/p/5139518.html
Copyright © 2020-2023  润新知