按下按钮时截取部分屏幕截图



我正在尝试通过按下按钮拍摄部分屏幕截图并将其保存到相机胶卷中。

我有从另一个问题中找到的代码,但按下按钮会使应用程序崩溃。

// Declare the snapshot boundaries
let top: CGFloat = 100
let bottom: CGFloat = 0
// The size of the cropped image
let size = CGSize(width: view.frame.size.width, height: view.frame.size.height - top - bottom)
// Start the context
UIGraphicsBeginImageContext(size)
// we are going to use context in a couple of places
let context = UIGraphicsGetCurrentContext()!
// Transform the context so that anything drawn into it is displaced "top" pixels up
context.translateBy(x: 0, y: -top)
// Draw the view into the context (this is the snapshot)
view.layer.render(in: context)
let snapshot = UIGraphicsGetImageFromCurrentImageContext()
// End the context (this is required to not leak resources)
UIGraphicsEndImageContext()
// Save to photos
UIImageWriteToSavedPhotosAlbum(snapshot!, nil, nil, nil)

在此函数中用参数调用您的屏幕截图视图:-

func imageWithView(view: UIView) -> UIImage {
     UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0)
     view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
     let img = UIGraphicsGetImageFromCurrentImageContext()
     UIGraphicsEndImageContext()
     return img
}

相关内容

  • 没有找到相关文章

最新更新