ios – Swift:如何使用子视图保存视图的屏幕截图?

我想用其子视图捕获视图.我正在使用此代码捕获屏幕:
let view = self.faceTrackerContainerView

UIGraphicsBeginImageContext(CGSizeMake(view.bounds.size.width,view.bounds.size.height))
UIGraphicsGetCurrentContext()!
self.view?.drawViewHierarchyInRect(view.frame,afterScreenUpdates: true)
let screenshot = UIGraphicsGetimageFromCurrentimageContext()
UIGraphicsEndImageContext()

UIImageWritetoSavedPhotosAlbum(screenshot,nil,nil)

这些代码捕获整个屏幕,包括位于视图前方的按钮.我尝试了这些代码,但它只捕获主视图,不包括其子视图:

let view = self.faceTrackerContainerView
let scale = UIScreen.mainScreen().scale
UIGraphicsBeginImageContextWithOptions(view.bounds.size,false,scale);

view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let screenshot = UIGraphicsGetimageFromCurrentimageContext()
UIGraphicsEndImageContext()

UIImageWritetoSavedPhotosAlbum(screenshot,nil)

我也从这里尝试了snapshotViewAfterScreenUpdates(true),但我不知道如何实现它.我在视图下方有一个collectionView,在视图顶部有一个按钮(两者都不在视图中).第一个代码使用button,collectionView和子视图捕获所有内容.第二个代码捕获没有子视图的视图.请让我知道我错过了什么,或者我能提供什么.谢谢!

解决方法

这对我有用,
func screenShotMethod() {
    //Create the UIImage
    UIGraphicsBeginImageContext(faceTrackerContainerView.frame.size)
    faceTrackerContainerView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetimageFromCurrentimageContext()
    UIGraphicsEndImageContext()

    //Save it to the camera roll
    UIImageWritetoSavedPhotosAlbum(image,nil)
}

Ref

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...