美文网首页
iOS 截图屏幕

iOS 截图屏幕

作者: 司徒新新 | 来源:发表于2020-09-01 14:52 被阅读0次

    OC版本

    - (UIImage*)screenSnapshot:(UIView *)view{
        UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        UIImage * iii = [[UIImage alloc] initWithData: UIImageJPEGRepresentation(image, 0.8)];
        return iii;
    }
    
    

    swift版本

    //截取屏幕
    func screenSnapshot(view: UIView) -> UIImage? {
        
        UIGraphicsBeginImageContextWithOptions(view.frame.size, false, 0.0)
        view.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        //截取指定范围
        return UIImage.init(data: image?.jpegData(compressionQuality: 0.5) ?? Data()) //image
        
    }
    
    
    //截取整个屏幕
    
    func screenSnapScreen() -> UIImage? {
      
        //截取整个屏幕
        guard let window = UIApplication.shared.keyWindow else { return nil }
        // 用下面这行而不是UIGraphicsBeginImageContext(),因为前者支持Retina
        UIGraphicsBeginImageContextWithOptions(window.bounds.size, false, 0.0)
        window.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
       
        return UIImage.init(data: image?.jpegData(compressionQuality: 0.5) ?? Data()) //image
    }
    

    相关文章

      网友评论

          本文标题:iOS 截图屏幕

          本文链接:https://www.haomeiwen.com/subject/tojvsktx.html