func startRecording() {
let timestamp = Int(Date().timeIntervalSince1970)
movieURL = URL(fileURLWithPath: NSTemporaryDirectory().appending("output_\(timestamp).mov"))
if FileManager.default.fileExists(atPath: movieURL!.path) {
try? FileManager.default.removeItem(at: movieURL!)
}
let ratio = ratioSelections[chooseRatio]
let outputWidth: CGFloat = 2160
let outputHeight: CGFloat = outputWidth * CGFloat(ratio.1) / CGFloat(ratio.0)
let outputSize = CGSize(width: outputWidth, height: outputHeight)
let inputAspectRatio: CGFloat = 0.55
let outputAspectRatio = CGFloat(ratio.0) / CGFloat(ratio.1)
var cropRegion = CGRect(x: 0, y: 0, width: 1, height: 1)
if outputAspectRatio < inputAspectRatio {
let widthRatio = outputAspectRatio / inputAspectRatio
let xOffset = (1 - widthRatio) / 2
cropRegion = CGRect(x: xOffset, y: 0, width: widthRatio, height: 1)
} else {
let heightRatio = inputAspectRatio / outputAspectRatio
let yOffset = (1 - heightRatio) / 2
cropRegion = CGRect(x: 0, y: yOffset, width: 1, height: heightRatio)
}
videoCamera.removeAllTargets()
filter.removeAllTargets()
guard let cropFilter = GPUImageCropFilter(cropRegion: cropRegion) else {
print("Không thể tạo crop filter")
return
}
let blendFilter = GPUImageAlphaBlendFilter()
blendFilter.mix = 1.0
let recordFilter = GPUImageFilter()
videoCamera.addTarget(cropFilter)
cropFilter.addTarget(recordFilter)
DispatchQueue.global(qos: .userInitiated).async {
// Move heavy processing to background thread
DispatchQueue.main.async {
let containerView = UIView(frame: CGRect(x: -10000, y: -10000, width: outputSize.width, height: outputSize.height))
containerView.backgroundColor = .clear
let cloneView = self.createLiveScaledCopyView(outputSize: outputSize)
containerView.addSubview(cloneView)
containerView.setNeedsLayout()
containerView.layoutIfNeeded()
guard let uiElement = GPUImageUIElement(view: containerView) else {
print("Không thể tạo GPUImageUIElement")
return
}
self.movieWriter = GPUImageMovieWriter(movieURL: self.movieURL!, size: outputSize)
self.movieWriter?.encodingLiveVideo = true
recordFilter.addTarget(blendFilter)
uiElement.addTarget(blendFilter)
blendFilter.addTarget(self.movieWriter)
let previewFilter = GPUImageFilter()
cropFilter.addTarget(previewFilter)
previewFilter.addTarget(self.gpuImageView)
recordFilter.frameProcessingCompletionBlock = { _, _ in
DispatchQueue.main.async {
uiElement.update()
self.videoCamera.useNextFrameForImageCapture() // Ensure we don't overrelease framebuffer
}
}
self.videoCamera.audioEncodingTarget = self.movieWriter
self.movieWriter?.startRecording()
print("
}
}
}```
The first time I press the record button (i.e., when **startRecording** is called), the app lags for about 4 seconds, then returns to normal — but sometimes, it even crashes immediately after the lag.
< /code>
*** Заканчивающее приложение из -за исключения Uncaught 'nsinternalinconsistencyException', причина: «попытался переопределить кадр -буфер, вы забыли назвать -UsenextFrameForimageCapture, прежде чем использовать -imageFromCurrentFrameBuffer? 0x19db41a7c 0x19f9b9ea0 0x101C36878 0x101C66F20 0x101C31E90 0x101C67748 0x101C69894 0x101C692E0 0x101A0A420 0x1019894 0x102592E0 0x101A0A420 0X1019F6200 0x1025444444444444444444444444444444444444444444444444444444444444н. 0x102566F38 0x10253C548 0x10253C484 0x1a06162b4 0x1a06140b0 0x1a0638700 0x179190 0x1a3256240 0x1a3254470 0x1a36B256240 0x1a3254470 0x1a36b256240 0x1a3254470 0x1a36b256240 0x1a3254470 0x36b256240 0x1a32544470 0x36. 0x101a27488 0x101a2758c 0x1c703bad8)
libc ++ abi: завершение из -за непред -исключения типа nsexception < /p>
I would truly appreciate any help or suggestions!
Подробнее здесь: https://stackoverflow.com/questions/796 ... y-gpuimage
Мобильная версия