150 lines
6.1 KiB
Swift
150 lines
6.1 KiB
Swift
import SwiftUI
|
||
import UIKit
|
||
|
||
/// Square crop for a playlist cover: pinch or slide to zoom, drag to place, pick the saved size.
|
||
struct CoverCropper: View {
|
||
let image: UIImage
|
||
let done: (Data?) -> Void
|
||
|
||
@State private var zoom: CGFloat = 1
|
||
@State private var offset: CGSize = .zero
|
||
@State private var dragStart: CGSize?
|
||
@State private var zoomStart: CGFloat?
|
||
@State private var side: CGFloat = 1
|
||
@State private var saving = false
|
||
@AppStorage("coverCropSize") private var outSize = 1200
|
||
|
||
private static let sizes = [(600, "Small"), (1200, "Medium"), (2000, "Large")]
|
||
private let maxZoom: CGFloat = 6
|
||
|
||
var body: some View {
|
||
NavigationStack {
|
||
VStack(spacing: 0) {
|
||
GeometryReader { geo in
|
||
let s = max(1, min(geo.size.width, geo.size.height) - 32)
|
||
ZStack {
|
||
Color.black
|
||
Image(uiImage: image)
|
||
.resizable()
|
||
.frame(width: shown(s).width, height: shown(s).height)
|
||
.offset(offset)
|
||
CropMask(side: s).allowsHitTesting(false)
|
||
}
|
||
.frame(width: geo.size.width, height: geo.size.height)
|
||
.contentShape(Rectangle())
|
||
.gesture(drag(s).simultaneously(with: pinch(s)))
|
||
.onAppear { side = s }
|
||
.onChange(of: s) { _, v in side = v; clamp(v) }
|
||
}
|
||
.clipped()
|
||
controls
|
||
}
|
||
.background(Color.black)
|
||
.navigationTitle("Crop Cover")
|
||
.navigationBarTitleDisplayMode(.inline)
|
||
.toolbarColorScheme(.dark, for: .navigationBar)
|
||
.toolbar {
|
||
ToolbarItem(placement: .cancellationAction) { Button("Cancel") { done(nil) } }
|
||
ToolbarItem(placement: .confirmationAction) {
|
||
if saving { ProgressView() } else { Button("Apply") { apply() }.bold() }
|
||
}
|
||
}
|
||
}
|
||
.preferredColorScheme(.dark)
|
||
}
|
||
|
||
private var controls: some View {
|
||
VStack(spacing: 14) {
|
||
HStack(spacing: 12) {
|
||
Image(systemName: "minus.magnifyingglass").foregroundStyle(.secondary)
|
||
Slider(value: Binding(get: { zoom }, set: { zoom = $0; clamp(side) }), in: 1...maxZoom)
|
||
Image(systemName: "plus.magnifyingglass").foregroundStyle(.secondary)
|
||
}
|
||
Picker("Size", selection: $outSize) {
|
||
ForEach(Self.sizes, id: \.0) { Text($0.1).tag($0.0) }
|
||
}
|
||
.pickerStyle(.segmented)
|
||
Text("\(outSize) × \(outSize) px").font(.caption).monospacedDigit().foregroundStyle(.secondary)
|
||
Button("Reset") { withAnimation(.snappy(duration: 0.2)) { zoom = 1; offset = .zero } }
|
||
.font(.subheadline)
|
||
}
|
||
.padding(.horizontal, 24).padding(.top, 12).padding(.bottom, 20)
|
||
}
|
||
|
||
/// The image's on-screen size: filling the square at zoom 1.
|
||
private func shown(_ s: CGFloat) -> CGSize {
|
||
let w = max(image.size.width, 1), h = max(image.size.height, 1)
|
||
let k = s / min(w, h) * zoom
|
||
return CGSize(width: w * k, height: h * k)
|
||
}
|
||
|
||
/// Keeps the square covered by the image.
|
||
private func clamp(_ s: CGFloat) {
|
||
let d = shown(s)
|
||
let mx = max(0, (d.width - s) / 2), my = max(0, (d.height - s) / 2)
|
||
offset = CGSize(width: min(max(offset.width, -mx), mx), height: min(max(offset.height, -my), my))
|
||
}
|
||
|
||
private func drag(_ s: CGFloat) -> some Gesture {
|
||
DragGesture(minimumDistance: 0)
|
||
.onChanged { v in
|
||
let start = dragStart ?? offset
|
||
if dragStart == nil { dragStart = start }
|
||
offset = CGSize(width: start.width + v.translation.width, height: start.height + v.translation.height)
|
||
clamp(s)
|
||
}
|
||
.onEnded { _ in dragStart = nil }
|
||
}
|
||
|
||
private func pinch(_ s: CGFloat) -> some Gesture {
|
||
MagnifyGesture()
|
||
.onChanged { v in
|
||
let start = zoomStart ?? zoom
|
||
if zoomStart == nil { zoomStart = start }
|
||
zoom = min(max(start * v.magnification, 1), maxZoom)
|
||
clamp(s)
|
||
}
|
||
.onEnded { _ in zoomStart = nil }
|
||
}
|
||
|
||
private func apply() {
|
||
let s = side
|
||
let d = shown(s)
|
||
let k = d.width / max(image.size.width, 1) // screen points per image point
|
||
// The square's top-left and side, in image points.
|
||
let x0 = (d.width / 2 - s / 2 - offset.width) / k
|
||
let y0 = (d.height / 2 - s / 2 - offset.height) / k
|
||
let crop = CGRect(x: x0, y: y0, width: s / k, height: s / k)
|
||
let img = image
|
||
let out = CGFloat(outSize)
|
||
saving = true
|
||
Task {
|
||
let data = await Task.detached(priority: .userInitiated) { CoverCropper.render(img, crop, out) }.value
|
||
saving = false
|
||
done(data)
|
||
}
|
||
}
|
||
|
||
nonisolated static func render(_ img: UIImage, _ crop: CGRect, _ out: CGFloat) -> Data? {
|
||
let f = out / max(crop.width, 1)
|
||
let fmt = UIGraphicsImageRendererFormat.default(); fmt.scale = 1; fmt.opaque = true
|
||
return UIGraphicsImageRenderer(size: CGSize(width: out, height: out), format: fmt).jpegData(withCompressionQuality: 0.85) { _ in
|
||
img.draw(in: CGRect(x: -crop.minX * f, y: -crop.minY * f, width: img.size.width * f, height: img.size.height * f))
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Dims everything outside the centered square and outlines it.
|
||
private struct CropMask: View {
|
||
let side: CGFloat
|
||
var body: some View {
|
||
GeometryReader { geo in
|
||
let r = CGRect(x: (geo.size.width - side) / 2, y: (geo.size.height - side) / 2, width: side, height: side)
|
||
Path { p in p.addRect(CGRect(origin: .zero, size: geo.size)); p.addRect(r) }
|
||
.fill(Color.black.opacity(0.55), style: FillStyle(eoFill: true))
|
||
Rectangle().stroke(Color.white.opacity(0.9), lineWidth: 1)
|
||
.frame(width: side, height: side).position(x: r.midX, y: r.midY)
|
||
}
|
||
}
|
||
}
|