我试图按下按钮进行部分截图并将其保存到相机胶卷。
我有从其他问题中找到的代码,但按下按钮会使应用程序崩溃。
// 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)
答案 0 :(得分:0)
使用参数调用此功能中的屏幕截图视图: -
func imageWithView(view: UIView) -> UIImage {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0)
view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}