zoukankan      html  css  js  c++  java
  • 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对象

  • 相关阅读:
    导弹拦截
    背包求方案的字典序
    分组背包
    关于字符串的简单dp
    dp进阶——饥饿的奶牛
    压缩维度oj P1173+P1174+P1164
    搜索——迭代加深
    委外倒冲领料
    QLIKVIEW-SALESORDERDELIVERYNOTICEOUTSTOCKINVOICE
    设置采购订单供应商权限设置
  • 原文地址:https://www.cnblogs.com/n1ckyxu/p/5139518.html
Copyright © 2011-2022 走看看