繁体   English   中英

保存UIView时保持空白图像...如何将UIView和子视图保存到相机胶卷上?

[英]Keep getting blank image when save UIView…how to save UIView & subviews onto camera roll?

我有一个UIView canvas ,我想在按UIBarButton shareBarButton时将其屏幕快照(及其子视图)保存在相机胶卷上。 但是,当我按下shareBarButton时,相机胶卷中出现的图像完全是黑屏。

对于为什么会发生这种情况的任何帮助将不胜感激! 我也UIViews接受有关将UIViews保存到相机胶卷上的其他(也许更好)方法的建议。

这是附加到shareBarButton的方法

func share(sender: UIBarButtonItem) {
    let masterpiece = canvas.snapshotViewAfterScreenUpdates(true)
    let image = convertViewToImage(masterpiece)
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}

这是将UIView canvas转换为UIImage的辅助函数。

func convertViewToImage(masterpiece: UIView) -> UIImage{
    UIGraphicsBeginImageContextWithOptions(masterpiece.bounds.size, masterpiece.opaque, UIScreen().scale)
    masterpiece.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

截图:

func captureView(view: UIView) -> UIImage {
        var size: CGSize = self.imageViewObject.image.size
        UIGraphicsBeginImageContext(size)
        self.imageViewObject.image.drawInRect(CGRectMake(0, 0, size.width, size.height))
        self.imageOverLayObject.image.drawInRect(CGRectMake(0, 0, size.width, size.height))
        var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM