美文网首页
Swift - 截屏

Swift - 截屏

作者: Hesse_Huang | 来源:发表于2016-03-03 20:50 被阅读1735次
func screenSnapshot(save save: Bool) -> UIImage? {
    
    guard let window = UIApplication.sharedApplication().keyWindow else { return nil }
    
    // 用下面这行而不是UIGraphicsBeginImageContext(),因为前者支持Retina
    UIGraphicsBeginImageContextWithOptions(window.bounds.size, false, 0.0)
    
    window.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    
    let image = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()
    
    if save { UIImageWriteToSavedPhotosAlbum(image, self, nil, nil) }
    
    return image
}

相关文章

网友评论

      本文标题:Swift - 截屏

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