Swift - 截屏

2016-03-03  本文已影响1735人  Hesse_Huang
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
}
上一篇下一篇

猜你喜欢

热点阅读