我有一个UIView canvas
我想保存它的屏幕截图,当我按下UIBarButton shareBarButton
. 这适用于模拟器,但是,它不会以我喜欢的方式生成图像:
- 理想情况下,我希望快照看起来像什么(屏幕顶部没有运营商、时间和电池状态除外)。
- 相机胶卷中的快照实际上是什么样子。
我希望快照看起来完全像它在 iPhone 屏幕上的样子。因此,如果形状的一部分超出屏幕,快照将仅捕获屏幕上可见的形状部分。我还希望快照的大小canvas
基本上是视图的大小,除了稍微短一点的高度:
canvas = UIView(frame: CGRectMake(0, 0, view.bounds.height, view.bounds.height-toolbar.bounds.height))
如果有人能说出我在创建快照时做错了什么,将不胜感激!
我的代码:
func share(sender: UIBarButtonItem) {
let masterpiece = canvas.snapshotViewAfterScreenUpdates(true)
let image = snapshot(masterpiece)
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
func snapshot(masterpiece: UIView) -> UIImage{
UIGraphicsBeginImageContextWithOptions(masterpiece.bounds.size, false, UIScreen.mainScreen().scale)
masterpiece.drawViewHierarchyInRect(masterpiece.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}