拍摄UIImageView的快照而不是主视图

时间:2018-05-21 09:02:34

标签: swift uiimageview

我在我的应用程序中查看按钮,用以下代码拍摄快照

open func takeScreenshot(_ shouldSave: Bool = true) -> UIImage? {
    var screenshotImage :UIImage?
    let layer = UIApplication.shared.keyWindow!.layer
    let scale = UIScreen.main.scale
    UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
    guard let context = UIGraphicsGetCurrentContext() else {return nil}
    layer.render(in:context)
    screenshotImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    if let image = screenshotImage, shouldSave {
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
    }
    return screenshotImage
}

但在同一个View中是UIImageView,我想拍摄这个UIImageView的快照。我该如何更改代码?当我使用此代码拍摄快照时,它将仅保存带有播放/暂停视频按钮的黑屏和带快照的按钮。

1 个答案:

答案 0 :(得分:0)

而是使用

let layer = UIApplication.shared.keyWindow!.layer

只需使用您的imageview图层

let layer = YourImageView.layer