微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐