1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import UIKit
struct AvatarCrop: Equatable, Sendable {
static let maximumZoom: CGFloat = 4
private(set) var quarterTurns = 0
private(set) var isFlipped = false
private(set) var zoom: CGFloat = 1
private(set) var offset = CGSize.zero
func scaledImageSize(for imageSize: CGSize) -> CGSize {
let shortestSide = min(imageSize.width, imageSize.height)
guard shortestSide.isFinite, shortestSide > 0 else { return .zero }
return CGSize(
width: imageSize.width / shortestSide * zoom,
height: imageSize.height / shortestSide * zoom
)
}
mutating func rotateClockwise() {
// A mirror reverses the direction of rotation.
quarterTurns = (quarterTurns + (isFlipped ? 3 : 1)) % 4
offset = CGSize(width: -offset.height, height: offset.width)
}
mutating func flipHorizontally() {
isFlipped.toggle()
offset.width = -offset.width
}
mutating func pan(by translation: CGSize, imageSize: CGSize) {
offset.width += translation.width
offset.height += translation.height
clampOffset(for: imageSize)
}
mutating func magnify(by factor: CGFloat, anchor: CGPoint, imageSize: CGSize) {
let newZoom = min(Self.maximumZoom, max(1, zoom * factor))
let scale = newZoom / zoom
offset = CGSize(
width: anchor.x + (offset.width - anchor.x) * scale,
height: anchor.y + (offset.height - anchor.y) * scale
)
zoom = newZoom
clampOffset(for: imageSize)
}
func jpegData(from image: UIImage) throws -> Data {
let side: CGFloat = 512
let canvas = CGSize(width: side, height: side)
let normalizedSize = scaledImageSize(for: image.size)
guard normalizedSize.width.isFinite, normalizedSize.height.isFinite,
normalizedSize.width > 0, normalizedSize.height > 0
else { throw ServiceError.invalidImage }
let imageSize = CGSize(width: normalizedSize.width * side, height: normalizedSize.height * side)
let format = UIGraphicsImageRendererFormat()
format.scale = 1
format.opaque = true
format.preferredRange = .standard
let result = UIGraphicsImageRenderer(size: canvas, format: format).image { renderer in
UIColor.black.setFill()
renderer.fill(CGRect(origin: .zero, size: canvas))
let context = renderer.cgContext
context.translateBy(x: (0.5 + offset.width) * side, y: (0.5 + offset.height) * side)
context.scaleBy(x: isFlipped ? -1 : 1, y: 1)
context.rotate(by: CGFloat(quarterTurns) * .pi / 2)
image.draw(in: CGRect(
x: -imageSize.width / 2, y: -imageSize.height / 2,
width: imageSize.width, height: imageSize.height
))
}
guard let data = result.jpegData(compressionQuality: 0.85) else { throw ServiceError.invalidImage }
guard data.count <= 1_000_000 else { throw ServiceError.imageTooLarge }
return data
}
private mutating func clampOffset(for imageSize: CGSize) {
let scaled = scaledImageSize(for: imageSize)
let isSideways = !quarterTurns.isMultiple(of: 2)
let width = isSideways ? scaled.height : scaled.width
let height = isSideways ? scaled.width : scaled.height
let horizontalLimit = max(0, (width - 1) / 2)
let verticalLimit = max(0, (height - 1) / 2)
offset.width = min(horizontalLimit, max(-horizontalLimit, offset.width))
offset.height = min(verticalLimit, max(-verticalLimit, offset.height))
}
}