mirror of
https://github.com/laosb/CropImage.git
synced 2025-05-01 08:01:09 +00:00
Compare commits
20 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
9a8b486f55 | ||
![]() |
481f59cf41 | ||
![]() |
7042102108 | ||
![]() |
a9672b8a33 | ||
![]() |
54861801e6 | ||
![]() |
6b33bcdbe4 | ||
![]() |
246b20d079 | ||
![]() |
672ec51d3e | ||
![]() |
ea5a5354fb | ||
![]() |
304a4c8e7e | ||
![]() |
517fb0c003 | ||
![]() |
4e0f333e2f | ||
![]() |
57ff1a7b86 | ||
![]() |
a74e54dd00 | ||
![]() |
1d66f10bff | ||
![]() |
c296f3e6e5 | ||
![]() |
030ac8cbde | ||
![]() |
e4d096dafb | ||
![]() |
e703e25200 | ||
![]() |
63a60f802a |
10 changed files with 220 additions and 85 deletions
2
.github/FUNDING.yml
vendored
Normal file
2
.github/FUNDING.yml
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
github: [laosb]
|
||||||
|
buy_me_a_coffee: laosb
|
|
@ -1,4 +1,4 @@
|
||||||
// swift-tools-version: 5.8
|
// swift-tools-version: 5.10
|
||||||
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
||||||
|
|
||||||
import PackageDescription
|
import PackageDescription
|
||||||
|
@ -7,7 +7,8 @@ let package = Package(
|
||||||
name: "CropImage",
|
name: "CropImage",
|
||||||
platforms: [
|
platforms: [
|
||||||
.iOS(.v14),
|
.iOS(.v14),
|
||||||
.macOS(.v13)
|
.macOS(.v13),
|
||||||
|
.visionOS(.v1)
|
||||||
],
|
],
|
||||||
products: [
|
products: [
|
||||||
// Products define the executables and libraries a package produces, and make them visible to other packages.
|
// Products define the executables and libraries a package produces, and make them visible to other packages.
|
||||||
|
@ -25,5 +26,6 @@ let package = Package(
|
||||||
.target(
|
.target(
|
||||||
name: "CropImage",
|
name: "CropImage",
|
||||||
dependencies: [])
|
dependencies: [])
|
||||||
]
|
],
|
||||||
|
swiftLanguageVersions: [.version("6"), .v5]
|
||||||
)
|
)
|
||||||
|
|
|
@ -5,13 +5,15 @@
|
||||||
|
|
||||||
A simple SwiftUI view where user can move and resize an image to a pre-defined size.
|
A simple SwiftUI view where user can move and resize an image to a pre-defined size.
|
||||||
|
|
||||||
Supports iOS 14.0 and above, or macOS Ventura 13.0 and above.
|
Supports iOS 14.0 and above, visionOS 1.0 and above or macOS Ventura 13.0 and above.
|
||||||
|
|
||||||
- Supports both iOS and macOS
|
- Supports iOS, visionOS and macOS
|
||||||
- Use `ImageRenderer` to render the cropped image, when possible
|
- Use `ImageRenderer` to render the cropped image, when possible
|
||||||
- Very lightweight
|
- Very lightweight
|
||||||
- (Optionally) bring your own crop UI
|
- (Optionally) bring your own crop UI
|
||||||
|
|
||||||
|
Full documentation is available on [Swift Package Index](https://swiftpackageindex.com/laosb/CropImage/main/documentation/cropimage). Be sure to choose the correct version.
|
||||||
|
|
||||||
<picture>
|
<picture>
|
||||||
<source media="(prefers-color-scheme: dark)" srcset="./Sources/CropImage/Documentation.docc/Resources/macos~dark.png">
|
<source media="(prefers-color-scheme: dark)" srcset="./Sources/CropImage/Documentation.docc/Resources/macos~dark.png">
|
||||||
<img alt="Preview on macOS" src="./Sources/CropImage/Documentation.docc/Resources/macos.png">
|
<img alt="Preview on macOS" src="./Sources/CropImage/Documentation.docc/Resources/macos.png">
|
||||||
|
|
|
@ -6,19 +6,32 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
#if os(iOS)
|
#if !os(macOS)
|
||||||
import UIKit
|
import UIKit
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// A view that allows the user to crop an image.
|
/// A view that allows the user to crop an image.
|
||||||
public struct CropImageView<Controls: View>: View {
|
public struct CropImageView<Controls: View, CutHole: View>: View {
|
||||||
public typealias ControlClosure<Controls> = (
|
/// Defines a custom view overlaid on the image cropper.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - offset: The offset binding of the image.
|
||||||
|
/// - scale: The scale binding of the image.
|
||||||
|
/// - rotation: The rotation binding of the image.
|
||||||
|
/// - crop: An async function to trigger crop action. Result will be delivered via ``onCrop``.
|
||||||
|
public typealias ControlClosure = (
|
||||||
_ offset: Binding<CGSize>,
|
_ offset: Binding<CGSize>,
|
||||||
_ scale: Binding<CGFloat>,
|
_ scale: Binding<CGFloat>,
|
||||||
_ rotation: Binding<Angle>,
|
_ rotation: Binding<Angle>,
|
||||||
_ crop: @escaping () async -> ()
|
_ crop: @escaping () async -> ()
|
||||||
) -> Controls
|
) -> Controls
|
||||||
|
|
||||||
|
/// Defines custom view that indicates the cut hole to users.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - targetSize: The size of the cut hole.
|
||||||
|
public typealias CutHoleClosure = (_ targetSize: CGSize) -> CutHole
|
||||||
|
|
||||||
/// Errors that could happen during the cropping process.
|
/// Errors that could happen during the cropping process.
|
||||||
public enum CropError: Error {
|
public enum CropError: Error {
|
||||||
/// SwiftUI `ImageRenderer` returned nil when calling `nsImage` or `uiImage`.
|
/// SwiftUI `ImageRenderer` returned nil when calling `nsImage` or `uiImage`.
|
||||||
|
@ -53,37 +66,51 @@ public struct CropImageView<Controls: View>: View {
|
||||||
///
|
///
|
||||||
/// The error should be a ``CropError``.
|
/// The error should be a ``CropError``.
|
||||||
public var onCrop: (Result<PlatformImage, Error>) -> Void
|
public var onCrop: (Result<PlatformImage, Error>) -> Void
|
||||||
/// A custom view overlaid on the image cropper.
|
var controls: ControlClosure
|
||||||
///
|
var cutHole: CutHoleClosure
|
||||||
/// - Parameters:
|
/// Create a ``CropImageView`` with a custom controls view and a custom cut hole.
|
||||||
/// - crop: An async function to trigger crop action. Result will be delivered via ``onCrop``.
|
|
||||||
public var controls: ControlClosure<Controls>
|
|
||||||
|
|
||||||
/// Create a ``CropImageView`` with a custom ``controls`` view.
|
|
||||||
public init(
|
public init(
|
||||||
image: PlatformImage,
|
image: PlatformImage,
|
||||||
targetSize: CGSize,
|
targetSize: CGSize,
|
||||||
targetScale: CGFloat = 1,
|
targetScale: CGFloat = 1,
|
||||||
fulfillTargetFrame: Bool = true,
|
fulfillTargetFrame: Bool = true,
|
||||||
onCrop: @escaping (Result<PlatformImage, Error>) -> Void,
|
onCrop: @escaping (Result<PlatformImage, Error>) -> Void,
|
||||||
@ViewBuilder controls: @escaping ControlClosure<Controls>
|
@ViewBuilder controls: @escaping ControlClosure,
|
||||||
|
@ViewBuilder cutHole: @escaping CutHoleClosure
|
||||||
) {
|
) {
|
||||||
self.image = image
|
self.image = image
|
||||||
self.targetSize = targetSize
|
self.targetSize = targetSize
|
||||||
self.targetScale = targetScale
|
self.targetScale = targetScale
|
||||||
self.onCrop = onCrop
|
self.onCrop = onCrop
|
||||||
self.controls = controls
|
self.controls = controls
|
||||||
|
self.cutHole = cutHole
|
||||||
}
|
}
|
||||||
/// Create a ``CropImageView`` with the default ``controls`` view.
|
/// Create a ``CropImageView`` with a custom controls view and default cut hole.
|
||||||
///
|
public init(
|
||||||
/// The default ``controls`` view is a simple overlay with a checkmark icon on the bottom-trailing corner to trigger crop action.
|
image: PlatformImage,
|
||||||
|
targetSize: CGSize,
|
||||||
|
targetScale: CGFloat = 1,
|
||||||
|
fulfillTargetFrame: Bool = true,
|
||||||
|
onCrop: @escaping (Result<PlatformImage, Error>) -> Void,
|
||||||
|
@ViewBuilder controls: @escaping ControlClosure
|
||||||
|
) where CutHole == DefaultCutHoleView {
|
||||||
|
self.image = image
|
||||||
|
self.targetSize = targetSize
|
||||||
|
self.targetScale = targetScale
|
||||||
|
self.onCrop = onCrop
|
||||||
|
self.controls = controls
|
||||||
|
self.cutHole = { targetSize in
|
||||||
|
DefaultCutHoleView(targetSize: targetSize)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Create a ``CropImageView`` with default UI elements.
|
||||||
public init(
|
public init(
|
||||||
image: PlatformImage,
|
image: PlatformImage,
|
||||||
targetSize: CGSize,
|
targetSize: CGSize,
|
||||||
targetScale: CGFloat = 1,
|
targetScale: CGFloat = 1,
|
||||||
fulfillTargetFrame: Bool = true,
|
fulfillTargetFrame: Bool = true,
|
||||||
onCrop: @escaping (Result<PlatformImage, Error>) -> Void
|
onCrop: @escaping (Result<PlatformImage, Error>) -> Void
|
||||||
) where Controls == DefaultControlsView {
|
) where Controls == DefaultControlsView, CutHole == DefaultCutHoleView {
|
||||||
self.image = image
|
self.image = image
|
||||||
self.targetSize = targetSize
|
self.targetSize = targetSize
|
||||||
self.targetScale = targetScale
|
self.targetScale = targetScale
|
||||||
|
@ -91,6 +118,9 @@ public struct CropImageView<Controls: View>: View {
|
||||||
self.controls = { $offset, $scale, $rotation, crop in
|
self.controls = { $offset, $scale, $rotation, crop in
|
||||||
DefaultControlsView(offset: $offset, scale: $scale, rotation: $rotation, crop: crop)
|
DefaultControlsView(offset: $offset, scale: $scale, rotation: $rotation, crop: crop)
|
||||||
}
|
}
|
||||||
|
self.cutHole = { targetSize in
|
||||||
|
DefaultCutHoleView(targetSize: targetSize)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@State private var offset: CGSize = .zero
|
@State private var offset: CGSize = .zero
|
||||||
|
@ -111,16 +141,16 @@ public struct CropImageView<Controls: View>: View {
|
||||||
fulfillTargetFrame: fulfillTargetFrame
|
fulfillTargetFrame: fulfillTargetFrame
|
||||||
)
|
)
|
||||||
.frame(width: targetSize.width, height: targetSize.height)
|
.frame(width: targetSize.width, height: targetSize.height)
|
||||||
if #available(iOS 16.0, macOS 13.0, *) {
|
if #available(iOS 16.0, macOS 13.0, visionOS 1.0, *) {
|
||||||
let renderer = ImageRenderer(content: snapshotView)
|
let renderer = ImageRenderer(content: snapshotView)
|
||||||
renderer.scale = targetScale
|
renderer.scale = targetScale
|
||||||
#if os(iOS)
|
#if !os(macOS)
|
||||||
if let image = renderer.uiImage {
|
if let image = renderer.uiImage {
|
||||||
return image
|
return image
|
||||||
} else {
|
} else {
|
||||||
throw CropError.imageRendererReturnedNil
|
throw CropError.imageRendererReturnedNil
|
||||||
}
|
}
|
||||||
#elseif os(macOS)
|
#else
|
||||||
if let image = renderer.nsImage {
|
if let image = renderer.nsImage {
|
||||||
return image
|
return image
|
||||||
} else {
|
} else {
|
||||||
|
@ -164,10 +194,6 @@ public struct CropImageView<Controls: View>: View {
|
||||||
.clipped()
|
.clipped()
|
||||||
}
|
}
|
||||||
|
|
||||||
var cutHole: some View {
|
|
||||||
DefaultCutHoleView(targetSize: targetSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
var viewSizeReadingView: some View {
|
var viewSizeReadingView: some View {
|
||||||
GeometryReader { geo in
|
GeometryReader { geo in
|
||||||
Rectangle()
|
Rectangle()
|
||||||
|
@ -192,14 +218,14 @@ public struct CropImageView<Controls: View>: View {
|
||||||
}
|
}
|
||||||
|
|
||||||
public var body: some View {
|
public var body: some View {
|
||||||
cutHole
|
cutHole(targetSize)
|
||||||
.background(underlyingImage)
|
.background(underlyingImage)
|
||||||
.background(viewSizeReadingView)
|
.background(viewSizeReadingView)
|
||||||
.overlay(control)
|
.overlay(control)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CropImageView_Previews: PreviewProvider {
|
#Preview {
|
||||||
struct PreviewView: View {
|
struct PreviewView: View {
|
||||||
@State private var targetSize: CGSize = .init(width: 100, height: 100)
|
@State private var targetSize: CGSize = .init(width: 100, height: 100)
|
||||||
@State private var result: Result<PlatformImage, Error>? = nil
|
@State private var result: Result<PlatformImage, Error>? = nil
|
||||||
|
@ -207,7 +233,7 @@ struct CropImageView_Previews: PreviewProvider {
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack {
|
VStack {
|
||||||
CropImageView(
|
CropImageView(
|
||||||
image: .init(contentsOfFile: "/Users/laosb/Downloads/png.png")!,
|
image: .previewImage,
|
||||||
targetSize: targetSize
|
targetSize: targetSize
|
||||||
) {
|
) {
|
||||||
result = $0
|
result = $0
|
||||||
|
@ -224,7 +250,7 @@ struct CropImageView_Previews: PreviewProvider {
|
||||||
case let .success(croppedImage):
|
case let .success(croppedImage):
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
Image(nsImage: croppedImage)
|
Image(nsImage: croppedImage)
|
||||||
#elseif os(iOS)
|
#else
|
||||||
Image(uiImage: croppedImage)
|
Image(uiImage: croppedImage)
|
||||||
#endif
|
#endif
|
||||||
case let .failure(error):
|
case let .failure(error):
|
||||||
|
@ -243,11 +269,9 @@ struct CropImageView_Previews: PreviewProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static var previews: some View {
|
return PreviewView()
|
||||||
PreviewView()
|
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
.frame(width: 500)
|
.frame(width: 500)
|
||||||
.frame(minHeight: 600)
|
.frame(minHeight: 600)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -19,22 +19,28 @@ public struct DefaultControlsView: View {
|
||||||
var rotateButton: some View {
|
var rotateButton: some View {
|
||||||
Button {
|
Button {
|
||||||
let roundedAngle = Angle.degrees((rotation.degrees / 90).rounded() * 90)
|
let roundedAngle = Angle.degrees((rotation.degrees / 90).rounded() * 90)
|
||||||
withAnimation {
|
withAnimation(.interactiveSpring()) {
|
||||||
rotation = roundedAngle + .degrees(90)
|
rotation = roundedAngle + .degrees(90)
|
||||||
}
|
}
|
||||||
} label: {
|
} label: {
|
||||||
Label("Rotate", systemImage: "rotate.right")
|
Label("Rotate", systemImage: "rotate.right")
|
||||||
.font(.title2)
|
.font(.title2)
|
||||||
|
#if !os(visionOS)
|
||||||
.foregroundColor(.accentColor)
|
.foregroundColor(.accentColor)
|
||||||
|
#endif
|
||||||
.labelStyle(.iconOnly)
|
.labelStyle(.iconOnly)
|
||||||
.padding(.horizontal, 6)
|
.padding(.horizontal, 6)
|
||||||
.padding(.vertical, 3)
|
.padding(.vertical, 3)
|
||||||
|
#if !os(visionOS)
|
||||||
.background(
|
.background(
|
||||||
RoundedRectangle(cornerRadius: 5, style: .continuous)
|
RoundedRectangle(cornerRadius: 5, style: .continuous)
|
||||||
.fill(.background)
|
.fill(.background)
|
||||||
)
|
)
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
#if !os(visionOS)
|
||||||
.buttonStyle(.plain)
|
.buttonStyle(.plain)
|
||||||
|
#endif
|
||||||
.padding()
|
.padding()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,14 +60,20 @@ public struct DefaultControlsView: View {
|
||||||
} } label: {
|
} } label: {
|
||||||
Label("Crop", systemImage: "checkmark.circle.fill")
|
Label("Crop", systemImage: "checkmark.circle.fill")
|
||||||
.font(.title2)
|
.font(.title2)
|
||||||
|
#if !os(visionOS)
|
||||||
.foregroundColor(.accentColor)
|
.foregroundColor(.accentColor)
|
||||||
|
#endif
|
||||||
.labelStyle(.iconOnly)
|
.labelStyle(.iconOnly)
|
||||||
.padding(1)
|
.padding(1)
|
||||||
|
#if !os(visionOS)
|
||||||
.background(
|
.background(
|
||||||
Circle().fill(.background)
|
Circle().fill(.background)
|
||||||
)
|
)
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
#if !os(visionOS)
|
||||||
.buttonStyle(.plain)
|
.buttonStyle(.plain)
|
||||||
|
#endif
|
||||||
.padding()
|
.padding()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ import SwiftUI
|
||||||
|
|
||||||
struct DefaultCutHoleShape: Shape {
|
struct DefaultCutHoleShape: Shape {
|
||||||
var size: CGSize
|
var size: CGSize
|
||||||
|
var isCircular = false
|
||||||
|
|
||||||
var animatableData: AnimatablePair<CGFloat, CGFloat> {
|
var animatableData: AnimatablePair<CGFloat, CGFloat> {
|
||||||
get { .init(size.width, size.height) }
|
get { .init(size.width, size.height) }
|
||||||
|
@ -30,22 +31,31 @@ struct DefaultCutHoleShape: Shape {
|
||||||
), size: size)
|
), size: size)
|
||||||
|
|
||||||
path.move(to: newRect.origin)
|
path.move(to: newRect.origin)
|
||||||
|
if isCircular {
|
||||||
|
path.addEllipse(in: newRect)
|
||||||
|
} else {
|
||||||
path.addLine(to: .init(x: newRect.maxX, y: newRect.minY))
|
path.addLine(to: .init(x: newRect.maxX, y: newRect.minY))
|
||||||
path.addLine(to: .init(x: newRect.maxX, y: newRect.maxY))
|
path.addLine(to: .init(x: newRect.maxX, y: newRect.maxY))
|
||||||
path.addLine(to: .init(x: newRect.minX, y: newRect.maxY))
|
path.addLine(to: .init(x: newRect.minX, y: newRect.maxY))
|
||||||
path.addLine(to: newRect.origin)
|
path.addLine(to: newRect.origin)
|
||||||
|
}
|
||||||
path.closeSubpath()
|
path.closeSubpath()
|
||||||
return Path(path)
|
return Path(path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DefaultCutHoleShape_Previews: PreviewProvider {
|
#Preview("Default") {
|
||||||
static var previews: some View {
|
|
||||||
VStack {
|
VStack {
|
||||||
DefaultCutHoleShape(size: .init(width: 100, height: 100))
|
DefaultCutHoleShape(size: .init(width: 100, height: 100))
|
||||||
.fill(style: FillStyle(eoFill: true))
|
.fill(style: FillStyle(eoFill: true))
|
||||||
.foregroundColor(.black.opacity(0.6))
|
.foregroundColor(.black.opacity(0.6))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
#Preview("Circular") {
|
||||||
|
VStack {
|
||||||
|
DefaultCutHoleShape(size: .init(width: 100, height: 100), isCircular: true)
|
||||||
|
.fill(style: FillStyle(eoFill: true))
|
||||||
|
.foregroundColor(.black.opacity(0.6))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -7,33 +7,64 @@
|
||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct DefaultCutHoleView: View {
|
/// The default cut hole view. Stroke and mask color can be adjusted.
|
||||||
|
public struct DefaultCutHoleView: View {
|
||||||
var targetSize: CGSize
|
var targetSize: CGSize
|
||||||
var showStroke = true
|
var strokeWidth: CGFloat
|
||||||
|
var maskColor: Color
|
||||||
|
var isCircular: Bool
|
||||||
|
|
||||||
|
/// Initialize a default rectangular or circular cut hole view with specified target size, stroke width and mask color.
|
||||||
|
public init(
|
||||||
|
targetSize: CGSize,
|
||||||
|
isCircular: Bool = false,
|
||||||
|
strokeWidth: CGFloat = 1,
|
||||||
|
maskColor: Color = .black.opacity(0.6)
|
||||||
|
) {
|
||||||
|
self.targetSize = targetSize
|
||||||
|
self.strokeWidth = strokeWidth
|
||||||
|
self.maskColor = maskColor
|
||||||
|
self.isCircular = isCircular
|
||||||
|
}
|
||||||
|
|
||||||
var background: some View {
|
var background: some View {
|
||||||
DefaultCutHoleShape(size: targetSize)
|
DefaultCutHoleShape(size: targetSize, isCircular: isCircular)
|
||||||
.fill(style: FillStyle(eoFill: true))
|
.fill(style: FillStyle(eoFill: true))
|
||||||
.foregroundColor(.black.opacity(0.6))
|
.foregroundColor(maskColor)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ViewBuilder
|
||||||
|
var strokeShape: some View {
|
||||||
|
if isCircular {
|
||||||
|
Circle()
|
||||||
|
.strokeBorder(style: .init(lineWidth: strokeWidth))
|
||||||
|
} else {
|
||||||
|
Rectangle()
|
||||||
|
.strokeBorder(style: .init(lineWidth: strokeWidth))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var stroke: some View {
|
var stroke: some View {
|
||||||
Rectangle()
|
strokeShape
|
||||||
.strokeBorder(style: .init(lineWidth: 1))
|
.frame(
|
||||||
.frame(width: targetSize.width + 2, height: targetSize.height + 2)
|
width: targetSize.width + strokeWidth * 2,
|
||||||
|
height: targetSize.height + strokeWidth * 2
|
||||||
|
)
|
||||||
.foregroundColor(.white)
|
.foregroundColor(.white)
|
||||||
}
|
}
|
||||||
|
|
||||||
var body: some View {
|
public var body: some View {
|
||||||
background
|
background
|
||||||
.allowsHitTesting(false)
|
.allowsHitTesting(false)
|
||||||
.overlay(showStroke ? stroke : nil)
|
.overlay(strokeWidth > 0 ? stroke : nil)
|
||||||
.animation(.default, value: targetSize)
|
.animation(.default, value: targetSize)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DefaultCutHoleView_Previews: PreviewProvider {
|
#Preview("Default") {
|
||||||
static var previews: some View {
|
|
||||||
DefaultCutHoleView(targetSize: .init(width: 100, height: 100))
|
DefaultCutHoleView(targetSize: .init(width: 100, height: 100))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#Preview("Circular") {
|
||||||
|
DefaultCutHoleView(targetSize: .init(width: 100, height: 100), isCircular: true)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,9 @@
|
||||||
|
|
||||||
A simple SwiftUI view where user can move and resize an image to a pre-defined size.
|
A simple SwiftUI view where user can move and resize an image to a pre-defined size.
|
||||||
|
|
||||||
Supports iOS 14.0 and above, or macOS Ventura 13.0 and above.
|
Supports iOS 14.0 and above, visionOS 1.0 and above or macOS Ventura 13.0 and above.
|
||||||
|
|
||||||
- Supports both iOS and macOS
|
- Supports iOS, visionOS and macOS
|
||||||
- Use `ImageRenderer` to render the cropped image, when possible
|
- Use `ImageRenderer` to render the cropped image, when possible
|
||||||
- Very lightweight
|
- Very lightweight
|
||||||
- (Optionally) bring your own crop UI
|
- (Optionally) bring your own crop UI
|
||||||
|
|
|
@ -11,12 +11,19 @@ import Foundation
|
||||||
import AppKit
|
import AppKit
|
||||||
/// The image object type, aliased to each platform.
|
/// The image object type, aliased to each platform.
|
||||||
///
|
///
|
||||||
/// On macOS, it's `NSImage` and on iOS it's `UIImage`.
|
/// On macOS, it's `NSImage` and on iOS/visionOS it's `UIImage`.
|
||||||
public typealias PlatformImage = NSImage
|
public typealias PlatformImage = NSImage
|
||||||
#elseif os(iOS)
|
extension PlatformImage {
|
||||||
|
@MainActor static let previewImage: PlatformImage = .init(contentsOf: URL(string: "file:///System/Library/Desktop%20Pictures/Hello%20Metallic%20Blue.heic")!)!
|
||||||
|
}
|
||||||
|
#else
|
||||||
import UIKit
|
import UIKit
|
||||||
/// The image object type, aliased to each platform.
|
/// The image object type, aliased to each platform.
|
||||||
///
|
///
|
||||||
/// On macOS, it's `NSImage` and on iOS it's `UIImage`.
|
/// On macOS, it's `NSImage` and on iOS/visionOS it's `UIImage`.
|
||||||
public typealias PlatformImage = UIImage
|
public typealias PlatformImage = UIImage
|
||||||
|
extension PlatformImage {
|
||||||
|
// This doesn't really work, but at least passes build.
|
||||||
|
static let previewImage: PlatformImage = .init(contentsOfFile: "/System/Library/Desktop Pictures/Hello Metallic Blue.heic")!
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -25,6 +25,11 @@ struct UnderlyingImageView: View {
|
||||||
@State private var tempOffset: CGSize = .zero
|
@State private var tempOffset: CGSize = .zero
|
||||||
@State private var tempScale: CGFloat = 1
|
@State private var tempScale: CGFloat = 1
|
||||||
@State private var tempRotation: Angle = .zero
|
@State private var tempRotation: Angle = .zero
|
||||||
|
@State private var scrolling: Bool = false
|
||||||
|
#if os(macOS)
|
||||||
|
@State private var hovering: Bool = false
|
||||||
|
@State private var scrollMonitor: Any?
|
||||||
|
#endif
|
||||||
|
|
||||||
// When rotated odd multiples of 90 degrees, we need to switch width and height of the image in calculations.
|
// When rotated odd multiples of 90 degrees, we need to switch width and height of the image in calculations.
|
||||||
var isRotatedOddMultiplesOf90Deg: Bool {
|
var isRotatedOddMultiplesOf90Deg: Bool {
|
||||||
|
@ -66,9 +71,15 @@ struct UnderlyingImageView: View {
|
||||||
clampedOffset.height = clampedOffset.height.clamped(to: yOffsetBounds(at: clampedScale))
|
clampedOffset.height = clampedOffset.height.clamped(to: yOffsetBounds(at: clampedScale))
|
||||||
|
|
||||||
if clampedScale != scale || clampedOffset != offset {
|
if clampedScale != scale || clampedOffset != offset {
|
||||||
withAnimation {
|
if scrolling {
|
||||||
scale = clampedScale
|
scale = clampedScale
|
||||||
offset = clampedOffset
|
offset = clampedOffset
|
||||||
|
scrolling = false
|
||||||
|
} else {
|
||||||
|
withAnimation(.interactiveSpring()) {
|
||||||
|
scale = clampedScale
|
||||||
|
offset = clampedOffset
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,10 +92,28 @@ struct UnderlyingImageView: View {
|
||||||
scale = min(widthScale, heightScale)
|
scale = min(widthScale, heightScale)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if os(macOS)
|
||||||
|
private func setupScrollMonitor() {
|
||||||
|
scrollMonitor = NSEvent.addLocalMonitorForEvents(matching: .scrollWheel) { event in
|
||||||
|
if hovering {
|
||||||
|
scrolling = true
|
||||||
|
scale = scale + event.scrollingDeltaY / 1000
|
||||||
|
}
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func removeScrollMonitor() {
|
||||||
|
if let scrollMonitor {
|
||||||
|
NSEvent.removeMonitor(scrollMonitor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
var imageView: Image {
|
var imageView: Image {
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
Image(nsImage: image)
|
Image(nsImage: image)
|
||||||
#elseif os(iOS)
|
#else
|
||||||
Image(uiImage: image)
|
Image(uiImage: image)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -94,6 +123,14 @@ struct UnderlyingImageView: View {
|
||||||
.gesture(dragGesture)
|
.gesture(dragGesture)
|
||||||
.gesture(magnificationgesture)
|
.gesture(magnificationgesture)
|
||||||
.gesture(rotationGesture)
|
.gesture(rotationGesture)
|
||||||
|
#if os(macOS)
|
||||||
|
.onAppear {
|
||||||
|
setupScrollMonitor()
|
||||||
|
}
|
||||||
|
.onDisappear {
|
||||||
|
removeScrollMonitor()
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
var dragGesture: some Gesture {
|
var dragGesture: some Gesture {
|
||||||
|
@ -104,7 +141,6 @@ struct UnderlyingImageView: View {
|
||||||
.onEnded { value in
|
.onEnded { value in
|
||||||
offset = offset + tempOffset
|
offset = offset + tempOffset
|
||||||
tempOffset = .zero
|
tempOffset = .zero
|
||||||
adjustToFulfillTargetFrame()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,7 +152,6 @@ struct UnderlyingImageView: View {
|
||||||
.onEnded { value in
|
.onEnded { value in
|
||||||
scale = scale * tempScale
|
scale = scale * tempScale
|
||||||
tempScale = 1
|
tempScale = 1
|
||||||
adjustToFulfillTargetFrame()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,18 +168,30 @@ struct UnderlyingImageView: View {
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
imageView
|
imageView
|
||||||
|
.rotationEffect(rotation + tempRotation)
|
||||||
.scaleEffect(scale * tempScale)
|
.scaleEffect(scale * tempScale)
|
||||||
.offset(offset + tempOffset)
|
.offset(offset + tempOffset)
|
||||||
.rotationEffect(rotation + tempRotation)
|
|
||||||
.overlay(interactionView)
|
.overlay(interactionView)
|
||||||
|
.clipped()
|
||||||
.onChange(of: viewSize) { newValue in
|
.onChange(of: viewSize) { newValue in
|
||||||
setInitialScale(basedOn: newValue)
|
setInitialScale(basedOn: newValue)
|
||||||
}
|
}
|
||||||
.clipped()
|
.onChange(of: scale) { _ in
|
||||||
|
adjustToFulfillTargetFrame()
|
||||||
|
}
|
||||||
|
.onChange(of: offset) { _ in
|
||||||
|
adjustToFulfillTargetFrame()
|
||||||
|
}
|
||||||
|
.onChange(of: rotation) { _ in
|
||||||
|
adjustToFulfillTargetFrame()
|
||||||
|
}
|
||||||
|
#if os(macOS)
|
||||||
|
.onHover { hovering = $0 }
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MoveAndScalableImageView_Previews: PreviewProvider {
|
#Preview {
|
||||||
struct PreviewView: View {
|
struct PreviewView: View {
|
||||||
@State private var offset: CGSize = .zero
|
@State private var offset: CGSize = .zero
|
||||||
@State private var scale: CGFloat = 1
|
@State private var scale: CGFloat = 1
|
||||||
|
@ -155,7 +202,7 @@ struct MoveAndScalableImageView_Previews: PreviewProvider {
|
||||||
offset: $offset,
|
offset: $offset,
|
||||||
scale: $scale,
|
scale: $scale,
|
||||||
rotation: $rotation,
|
rotation: $rotation,
|
||||||
image: .init(contentsOfFile: "/Users/laosb/Downloads/png.png")!,
|
image: .previewImage,
|
||||||
viewSize: .init(width: 200, height: 100),
|
viewSize: .init(width: 200, height: 100),
|
||||||
targetSize: .init(width: 100, height: 100),
|
targetSize: .init(width: 100, height: 100),
|
||||||
fulfillTargetFrame: true
|
fulfillTargetFrame: true
|
||||||
|
@ -164,7 +211,5 @@ struct MoveAndScalableImageView_Previews: PreviewProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static var previews: some View {
|
return PreviewView()
|
||||||
PreviewView()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue