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)*/
}
}
}
参考链接
发表评论