Код: Выделить всё
func startVideo() {
videoCapture = VideoCapture()
videoCapture.delegate = self
videoCapture.setUp(sessionPreset: .photo) { success in
// .hd4K3840x2160 or .photo (4032x3024) Warning: 4k may not work on all devices i.e. 2019 iPod
if success {
// Add the video preview into the UI.
if let previewLayer = self.videoCapture.previewLayer {
self.view!.layer.addSublayer(previewLayer)
self.videoCapture.previewLayer?.frame = self.view!.bounds // resize preview layer
}
// Add the bounding box layers to the UI, on top of the video preview.
for box in self.boundingBoxViews {
box.addToLayer(self.view!.layer)
}
// Once everything is set up, we can start capturing live video.
self.videoCapture.start()
}
}
}
Похоже, это не работает.
Код: Выделить всё
func snapScreen() {
let bounds = UIScreen.main.bounds
//UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
let context = UIGraphicsGetCurrentContext()
//self.view!.drawHierarchy(in: bounds, afterScreenUpdates: true)
self.view!.layer.render(in: context!)
let img = UIGraphicsGetImageFromCurrentImageContext()
saveScreenImage(image: img!)
UIGraphicsEndImageContext()
}
В сообщении CALayer.render(in: Context) говорится, что все слои и подслои будут отображаться в контексте.
Хорошо, поскольку это не сработало, я подумал, что videoPreviewLayer отсутствует в подслоях, поэтому я перебрал все подслои, и это, похоже, тоже не работает.
Код: Выделить всё
func snapScreen() {
let bounds = UIScreen.main.bounds
//UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
let context = UIGraphicsGetCurrentContext()
//self.view!.drawHierarchy(in: bounds, afterScreenUpdates: true)
for layer in self.view!.layer.sublayers! {
layer.render(in: context!)
}
let img = UIGraphicsGetImageFromCurrentImageContext()
saveScreenImage(image: img!)
UIGraphicsEndImageContext()
}
- Это не работает, либо захватывается только слой предварительного просмотра.
- Это медленно из-за затрат на рисование изображения, и снова сохраняется только слой предварительного просмотра, а не изображение.
Код: Выделить всё
extension CameraViewController: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if let error = error {
print("error occurred : \(error.localizedDescription)")
}
DispatchQueue.main.async {
if let dataImage = photo.fileDataRepresentation() {
print(UIImage(data: dataImage)?.size as Any)
let dataProvider = CGDataProvider(data: dataImage as CFData)
let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
let image = UIImage(cgImage: cgImageRef, scale: 0.5, orientation: UIImage.Orientation.right)
let bounds = UIScreen.main.bounds
//UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
let context = UIGraphicsGetCurrentContext()
UIGraphicsPushContext(context!)
//self.view!.drawHierarchy(in: bounds, afterScreenUpdates: true)
image.draw(at: CGPoint(x: 0, y: 0))
UIGraphicsPopContext()
context?.saveGState()
self.view!.layer.render(in: context!)
context?.restoreGState()
let newImg = UIGraphicsGetImageFromCurrentImageContext()
UIImageWriteToSavedPhotosAlbum(newImg!, nil, nil, nil);
UIGraphicsEndImageContext()
// Save to camera roll
} else {
print("AVCapturePhotoCaptureDelegate Error")
}
}
}
}

Что я получаю в CapturePhotoDelegate

Что я получаю в SnapScreen

Если у кого-то есть идеи, что я делаю неправильно, дайте мне знать. Единственный аспект, который мне может не хватать, это то, что в snapScreen() я не обращаюсь к буферу изображения, поскольку он уже загружен в представление. Возможно, я ошибаюсь.
Подробнее здесь: https://stackoverflow.com/questions/782 ... lay-layers
Мобильная версия