iOS 获取设备相机所有可用分辨率 supportedMaxPhotoDimensions 并配置 maxPhotoDimensions

iOS 16 以上使用 isHighResolutionPhotoEnabled 需要配置 maxPhotoDimensions, 以下是根据设备 supportedMaxPhotoDimensions 获取最高拍摄分辨率的办法:

private var bestPhotoDimension: CMVideoDimensions?    
private var captureDevice: AVCaptureDevice? {
        didSet {
            guard let captureDevice = captureDevice else { return }
            logger.debug("Using capture device: \(captureDevice.localizedName)")
            var bestFormat: AVCaptureDevice.Format?
            

            for format in captureDevice.formats {
                for dimension in format.supportedMaxPhotoDimensions {
                    if dimension.width > bestPhotoDimension?.width ?? 0 && dimension.height > bestPhotoDimension?.height ?? 0 {
                        bestFormat = format
                        bestPhotoDimension = dimension
                        print(bestPhotoDimension!)
                    }
                }
            }
            
            if let bestFormat = bestFormat {
                do {
                    try captureDevice.lockForConfiguration()
                    
                    // Set the device's active format.
                    captureDevice.activeFormat = bestFormat
                    captureDevice.unlockForConfiguration()
                } catch {
                    // Handle error.
                }
            }
            updateSessionForCaptureDevice(captureDevice)
        }
    }

例如阁下要在拍摄函数中需要设置使用最高分辨率拍摄:

    // Code sample excerpt from https://developer.apple.com/tutorials/sample-apps/
func takePhoto() {
        guard let photoOutput = self.photoOutput else { return }
        /*#-code-walkthrough(photoflow.takePhoto)*/
        
        sessionQueue.async {
        
            /*#-code-walkthrough(photoflow.photoSettings)*/
            var photoSettings = AVCapturePhotoSettings()
            /*#-code-walkthrough(photoflow.photoSettings)*/

            if photoOutput.availablePhotoCodecTypes.contains(.hevc) {
                photoSettings = AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.hevc])
            }
            
            let isFlashAvailable = self.deviceInput?.device.isFlashAvailable ?? false
            photoSettings.flashMode = isFlashAvailable ? .auto : .off
//            photoSettings.isHighResolutionPhotoEnabled = true
            if let bestPhotoDimension = self.bestPhotoDimension {
                photoSettings.maxPhotoDimensions = bestPhotoDimension
            }
            if let previewPhotoPixelFormatType = photoSettings.availablePreviewPhotoPixelFormatTypes.first {
                photoSettings.previewPhotoFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPhotoPixelFormatType]
            }
            photoSettings.photoQualityPrioritization = .balanced
            
            if let photoOutputVideoConnection = photoOutput.connection(with: .video) {
                if photoOutputVideoConnection.isVideoOrientationSupported,
                    let videoOrientation = self.videoOrientationFor(self.deviceOrientation) {
                    photoOutputVideoConnection.videoOrientation = videoOrientation
                }
            }
            
            /*#-code-walkthrough(photoflow.capturePhoto)*/
            photoOutput.capturePhoto(with: photoSettings, delegate: self)
            /*#-code-walkthrough(photoflow.capturePhoto)*/
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值