Fix captured photo quality

This commit is contained in:
Ilya Laktyushin 2023-07-21 22:16:43 +02:00
parent c9c694d585
commit d3ee489694
4 changed files with 27 additions and 9 deletions

View File

@ -69,6 +69,7 @@ final class CameraDevice {
var maxHeight: Int32 = 0 var maxHeight: Int32 = 0
var hasSecondaryZoomLevels = false var hasSecondaryZoomLevels = false
var candidates: [AVCaptureDevice.Format] = [] var candidates: [AVCaptureDevice.Format] = []
var photoCandidates: [AVCaptureDevice.Format] = []
outer: for format in device.formats { outer: for format in device.formats {
if format.mediaType != .video || format.value(forKey: "isPhotoFormat") as? Bool == true { if format.mediaType != .video || format.value(forKey: "isPhotoFormat") as? Bool == true {
continue continue
@ -94,8 +95,14 @@ final class CameraDevice {
if #available(iOS 16.0, *), !format.secondaryNativeResolutionZoomFactors.isEmpty { if #available(iOS 16.0, *), !format.secondaryNativeResolutionZoomFactors.isEmpty {
hasSecondaryZoomLevels = true hasSecondaryZoomLevels = true
candidates.append(format) candidates.append(format)
if format.isHighPhotoQualitySupported {
photoCandidates.append(format)
}
} else if !hasSecondaryZoomLevels { } else if !hasSecondaryZoomLevels {
candidates.append(format) candidates.append(format)
if #available(iOS 15.0, *), format.isHighPhotoQualitySupported {
photoCandidates.append(format)
}
} }
} }
} }
@ -103,6 +110,15 @@ final class CameraDevice {
if !candidates.isEmpty { if !candidates.isEmpty {
var bestFormat: AVCaptureDevice.Format? var bestFormat: AVCaptureDevice.Format?
photoOuter: for format in photoCandidates {
for range in format.videoSupportedFrameRateRanges {
if range.maxFrameRate > maxFramerate {
continue photoOuter
}
bestFormat = format
}
}
if bestFormat == nil {
outer: for format in candidates { outer: for format in candidates {
for range in format.videoSupportedFrameRateRanges { for range in format.videoSupportedFrameRateRanges {
if range.maxFrameRate > maxFramerate { if range.maxFrameRate > maxFramerate {
@ -111,6 +127,7 @@ final class CameraDevice {
bestFormat = format bestFormat = format
} }
} }
}
if bestFormat == nil { if bestFormat == nil {
bestFormat = candidates.last bestFormat = candidates.last
} }

View File

@ -96,6 +96,10 @@ final class CameraOutput: NSObject {
super.init() super.init()
if #available(iOS 13.0, *) {
self.photoOutput.maxPhotoQualityPrioritization = .balanced
}
self.videoOutput.alwaysDiscardsLateVideoFrames = false self.videoOutput.alwaysDiscardsLateVideoFrames = false
self.videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey: kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange] as [String : Any] self.videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey: kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange] as [String : Any]
} }
@ -246,15 +250,11 @@ final class CameraOutput: NSObject {
settings.previewPhotoFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPhotoPixelFormatType] settings.previewPhotoFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPhotoPixelFormatType]
} }
if #available(iOS 13.0, *) { if #available(iOS 13.0, *) {
if self.exclusive {
if self.photoOutput.maxPhotoQualityPrioritization != .speed { if self.photoOutput.maxPhotoQualityPrioritization != .speed {
settings.photoQualityPrioritization = .balanced settings.photoQualityPrioritization = .balanced
} else { } else {
settings.photoQualityPrioritization = .speed settings.photoQualityPrioritization = .speed
} }
} else {
settings.photoQualityPrioritization = .speed
}
} }
let uniqueId = settings.uniqueID let uniqueId = settings.uniqueID

View File

@ -27,6 +27,7 @@ private func prerenderTextTransformations(entity: DrawingTextEntity, image: UIIm
let newImage = generateImage(newSize, contextGenerator: { size, context in let newImage = generateImage(newSize, contextGenerator: { size, context in
context.setAllowsAntialiasing(true) context.setAllowsAntialiasing(true)
context.setShouldAntialias(true) context.setShouldAntialias(true)
context.interpolationQuality = .high
context.clear(CGRect(origin: .zero, size: size)) context.clear(CGRect(origin: .zero, size: size))
context.translateBy(x: newSize.width * 0.5, y: newSize.height * 0.5) context.translateBy(x: newSize.width * 0.5, y: newSize.height * 0.5)
context.rotate(by: angle) context.rotate(by: angle)

View File

@ -249,7 +249,7 @@ public final class AccountContextImpl: AccountContext {
self.account = account self.account = account
self.engine = TelegramEngine(account: account) self.engine = TelegramEngine(account: account)
self.userLimits = EngineConfiguration.UserLimits(UserLimitsConfiguration.defaultValue, isPremium: false) self.userLimits = EngineConfiguration.UserLimits(UserLimitsConfiguration.defaultValue)
self.downloadedMediaStoreManager = DownloadedMediaStoreManagerImpl(postbox: account.postbox, accountManager: sharedContext.accountManager) self.downloadedMediaStoreManager = DownloadedMediaStoreManagerImpl(postbox: account.postbox, accountManager: sharedContext.accountManager)