0

我有一个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

}
4

1 回答 1

1

在第一个实例中,我会尝试对 UIWindow 进行快照以查看是否可以解决您的问题:

这是我使用的 UIWindow 扩展(不是专门用于相机工作) - 试试看。

import UIKit

extension UIWindow {

  func capture() -> UIImage {

      UIGraphicsBeginImageContextWithOptions(self.frame.size, self.opaque, UIScreen.mainScreen().scale)
      self.layer.renderInContext(UIGraphicsGetCurrentContext()!)
      let image = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()

      return image
  }

}

我这样称呼它:

let window: UIWindow! = UIApplication.sharedApplication().keyWindow

let windowImage = window.capture()
于 2016-01-04T17:31:09.803 回答