0.10.6
This commit is contained in:
commit
92660a4ba0
228 files changed
+24496
No files matched your search
@@ -0,0 +1,49 @@
|
||||
import Foundation
|
||||
import ImageIO
|
||||
import UniformTypeIdentifiers
|
||||
|
||||
enum AvatarImageProcessor {
|
||||
static func pngData(from sourceData: Data) throws -> Data {
|
||||
guard let source = CGImageSourceCreateWithData(sourceData as CFData, nil),
|
||||
let image = CGImageSourceCreateThumbnailAtIndex(
|
||||
source,
|
||||
0,
|
||||
[
|
||||
kCGImageSourceCreateThumbnailFromImageAlways: true,
|
||||
kCGImageSourceCreateThumbnailWithTransform: true,
|
||||
kCGImageSourceThumbnailMaxPixelSize: 512
|
||||
] as CFDictionary
|
||||
) else {
|
||||
throw AvatarImageError.invalidImage
|
||||
}
|
||||
|
||||
let output = NSMutableData()
|
||||
guard let destination = CGImageDestinationCreateWithData(
|
||||
output,
|
||||
UTType.png.identifier as CFString,
|
||||
1,
|
||||
nil
|
||||
) else {
|
||||
throw AvatarImageError.encodingFailed
|
||||
}
|
||||
CGImageDestinationAddImage(destination, image, nil)
|
||||
guard CGImageDestinationFinalize(destination) else {
|
||||
throw AvatarImageError.encodingFailed
|
||||
}
|
||||
return output as Data
|
||||
}
|
||||
}
|
||||
|
||||
private enum AvatarImageError: LocalizedError {
|
||||
case invalidImage
|
||||
case encodingFailed
|
||||
|
||||
var errorDescription: String? {
|
||||
switch self {
|
||||
case .invalidImage:
|
||||
return "Не удалось прочитать выбранное изображение."
|
||||
case .encodingFailed:
|
||||
return "Не удалось подготовить аватар в формате PNG."
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user