我在Stackoverflow上看到了几十个类似的问题。合并缩减视图的图像与子视图的图像保留原始大小
尽管他们都没有帮助。我有一个应用程序,在其中制作照片,然后在照片上添加一些图像。然后,我需要将拍摄的照片与我放在其上的图像一起保存。这里是我的结构看起来像:
正如你可以看到我有Photo View
内Photo Scroll View
。作为子视图,每个添加的贴纸进入Photo Scroll View
。
现在,我该如何合并来自Photo Scroll View
的所有图像到一个图像中。到目前为止,我已经设法将它保存为一个绝对太小的屏幕大小。
func saveImage(_ sender: Any) {
//Create the UIImage
UIGraphicsBeginImageContext(photoScrollView.frame.size)
photoScrollView.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
//Save it to the camera roll
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
此外,我试图通过创建快照。在这种情况下,图像只包含白色屏幕,没有其他东西(并且它不是任何视图的背景颜色)。
func saveImage(_ sender: Any) {
let snapShot:UIView = self.photoScrollView.snapshotView(afterScreenUpdates: true)!
UIGraphicsBeginImageContext(self.photoScrollView.bounds.size)
snapShot.drawHierarchy(in: self.photoScrollView.bounds, afterScreenUpdates: true)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
任何人都可以帮助我吗?
使用的第一次尝试,调用'photoScrollView.draw(RECT:photoScrollView.frame)' –
@gkchristopher我换成'photoScrollView.layer.render(在:UIGraphicsGetCurrentContext ()!)'带'photoScrollView.draw(rect:photoScrollView.frame)''。没有帮助。结果只是一张白色图片,而'photoScrollView'背景为绿色。 –