我想用其子视图捕获视图.我正在使用此代码捕获屏幕:
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) }