Hi iOS community need your help. I am working on an application where I am capturing photo from the back camera using AVCaptureSession. It is working fine with the devices running iOS17+ but I am facing an error on device iPhone X running iOS 16.7.4
ERROR:
` error: Optional(Error Domain=AVFoundationErrorDomain Code=-11803 "Cannot Record" UserInfo={NSUnderlyingError=0x283f0b780 {Error Domain=NSOSStatusErrorDomain Code=-16409 "(null)"}, NSLocalizedRecoverySuggestion=Try recording again., AVErrorRecordingFailureDomainKey=3, NSLocalizedDescription=Cannot Record})
My Code here:
final class CedulaScanningVC: UIViewController {
var captureSession: AVCaptureSession!
var stillImageOutput: AVCapturePhotoOutput!
var videoPreviewLayer: AVCaptureVideoPreviewLayer!
var delegate: ScanCedulaDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.captureSession.stopRunning()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setupCamera()
}
// MARK: - Configure Camera
func setupCamera() {
captureSession = AVCaptureSession()
captureSession.sessionPreset = .medium
guard let backCamera = AVCaptureDevice.default(for: AVMediaType.video)
else {
print("Unable to access back camera!")
return
}
let input: AVCaptureDeviceInput
do {
input = try AVCaptureDeviceInput(device: backCamera)
//Step 9
stillImageOutput = AVCapturePhotoOutput()
if captureSession.canAddInput(input) && captureSession.canAddOutput(stillImageOutput) {
captureSession.addInput(input)
captureSession.addOutput(stillImageOutput)
setupLivePreview()
}
}
catch let error {
print("Error Unable to initialize back camera: \(error.localizedDescription)")
}
}
func setupLivePreview() {
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer.videoGravity = .resizeAspectFill
videoPreviewLayer.connection?.videoOrientation = .portrait
self.view.layer.addSublayer(videoPreviewLayer)
//Step12
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
self?.captureSession.startRunning()
//Step 13
DispatchQueue.main.async {
self?.videoPreviewLayer.frame = self?.view.bounds ?? .zero
}
}
}
func failed() {
let ac = UIAlertController(title: "Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
captureSession = nil
}
// MARK: - actions
func cameraButtonPressed() {
let settings = AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])
stillImageOutput.capturePhoto(with: settings, delegate: self)
}
}
extension CedulaScanningVC: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
print("error: \(error)")
captureSession.stopRunning()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { [weak self] in
guard let self = self else {return}
guard let imageData = photo.fileDataRepresentation()
else {
print("NO image captured")
return
}
let image = UIImage(data: imageData)
self.delegate?.capturedImage(image: image)
}
}
}
I don't know what am doing wrong ?
Post
Replies
Boosts
Views
Activity
Hi I am using UIActivityViewController to share content on social networks but facing one issue. The issue is when i select the share button the UIActivityViewController gets appear and I select the Facebook option.
I select the Facebook option in the sharing sheet -> Facebook sharing view appears with two options (Cancel and Next). When I select the Cancel button giving me another action sheet to discard or keep the post. When i select to discard the Action Sheet get disappear but the Facebook sharing view still keep showing and there is no way to go back.
Second when I press the next button in the Facebook sharing view -> another Facebook view gets appear with two buttons (Cancel and Share).... When I click the share button content get share on Facebook and black screen appears with a check in circle and text "Shared to Facebook!" .. And the application get stuck on this Screen.
Below is my code, Any help would be appreciated, I am also using the activityViewController!.completionWithItemsHandler but this closure not getting called in case of Facebook.
let url = "\(UserDefaults.baseURL)/pl/\(resource)/\(sharingId)"
// set up activity view controller
let imageToShare = [ url ]
activityViewController = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil)
activityViewController!.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
// exclude some activity types from the list (optional)
activityViewController!.excludedActivityTypes = [UIActivity.ActivityType.airDrop]
activityViewController!.completionWithItemsHandler = { (activityType: UIActivity.ActivityType?, completed:
Bool, arrayReturnedItems: [Any]?, error: Error?) in
if completed {
print("share completed")
self.dismiss(animated: true)
return
} else {
print("cancel")
self.dismiss(animated: true)
}
if let shareError = error {
print("error while sharing: \(shareError.localizedDescription)")
}
}
// present the view controller
self.present(activityViewController!, animated: true, completion: nil)