Method to find device's camera resolution iOS

后端 未结 5 2145
北荒
北荒 2020-12-04 11:20

Whats the best method to find the image resolution going to be captured using setting AVCaptureSessionPresetPhoto.
I am trying to find the resolution before

5条回答
  •  情深已故
    2020-12-04 12:25

    With the function below, you can programmatically get the resolution from activeFormat before capture begins, though not before adding inputs and outputs: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVCaptureDevice_Class/index.html#//apple_ref/occ/instp/AVCaptureDevice/activeFormat

    private func getCaptureResolution() -> CGSize {
        // Define default resolution
        var resolution = CGSize(width: 0, height: 0)
    
        // Get cur video device
        let curVideoDevice = useBackCamera ? backCameraDevice : frontCameraDevice
    
        // Set if video portrait orientation
        let portraitOrientation = orientation == .Portrait || orientation == .PortraitUpsideDown
    
        // Get video dimensions
        if let formatDescription = curVideoDevice?.activeFormat.formatDescription {
            let dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription)
            resolution = CGSize(width: CGFloat(dimensions.width), height: CGFloat(dimensions.height))
            if portraitOrientation {
                resolution = CGSize(width: resolution.height, height: resolution.width)
            }
        }
    
        // Return resolution
        return resolution
    }
    

提交回复
热议问题