Skip to content

Commit

Permalink
feat: Initial image size.
Browse files Browse the repository at this point in the history
  • Loading branch information
laosb committed Aug 10, 2023
1 parent b42b532 commit edc6a5c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
24 changes: 21 additions & 3 deletions Sources/CropImage/CropImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public struct CropImageView<Controls: View>: View {

/// The image to crop.
public var image: PlatformImage
/// The region in which the image is initially fitted in, in points.
public var initialImageSize: CGSize
/// The intended size of the cropped image, in points.
public var targetSize: CGSize
/// The intended scale of the cropped image.
Expand All @@ -56,12 +58,14 @@ public struct CropImageView<Controls: View>: View {
/// Create a ``CropImageView`` with a custom ``controls`` view.
public init(
image: PlatformImage,
initialImageSize: CGSize,
targetSize: CGSize,
targetScale: CGFloat = 1,
onCrop: @escaping (Result<PlatformImage, Error>) -> Void,
@ViewBuilder controls: @escaping ControlClosure<Controls>
) {
self.image = image
self.initialImageSize = initialImageSize
self.targetSize = targetSize
self.targetScale = targetScale
self.onCrop = onCrop
Expand All @@ -72,11 +76,13 @@ public struct CropImageView<Controls: View>: View {
/// The default ``controls`` view is a simple overlay with a checkmark icon on the bottom-trailing corner to trigger crop action.
public init(
image: PlatformImage,
initialImageSize: CGSize,
targetSize: CGSize,
targetScale: CGFloat = 1,
onCrop: @escaping (Result<PlatformImage, Error>) -> Void
) where Controls == DefaultControlsView {
self.image = image
self.initialImageSize = initialImageSize
self.targetSize = targetSize
self.targetScale = targetScale
self.onCrop = onCrop
Expand All @@ -95,7 +101,8 @@ public struct CropImageView<Controls: View>: View {
offset: $offset,
scale: $scale,
rotation: $rotation,
image: image
image: image,
initialImageSize: initialImageSize
)
.frame(width: targetSize.width, height: targetSize.height)
if #available(iOS 16.0, macOS 13.0, *) {
Expand Down Expand Up @@ -143,7 +150,8 @@ public struct CropImageView<Controls: View>: View {
offset: $offset,
scale: $scale,
rotation: $rotation,
image: image
image: image,
initialImageSize: initialImageSize
)
RectHoleShape(size: targetSize)
.fill(style: FillStyle(eoFill: true))
Expand All @@ -163,16 +171,25 @@ public struct CropImageView<Controls: View>: View {

struct CropImageView_Previews: PreviewProvider {
struct PreviewView: View {
@State private var initialImageSize: CGSize = .init(width: 200, height: 200)
@State private var targetSize: CGSize = .init(width: 100, height: 100)
@State private var result: Result<PlatformImage, Error>? = nil

var body: some View {
VStack {
CropImageView(
image: .init(contentsOfFile: "/Users/laosb/Downloads/png.png")!,
initialImageSize: initialImageSize,
targetSize: targetSize
) { result = $0 }
Form {
Section {
TextField("Width", value: $initialImageSize.width, formatter: NumberFormatter())
TextField("Height", value: $initialImageSize.height, formatter: NumberFormatter())
} header: {
Text("Initial Image Size")
Text("The image will be fitted into this region.")
}
Section {
TextField("Width", value: $targetSize.width, formatter: NumberFormatter())
TextField("Height", value: $targetSize.height, formatter: NumberFormatter())
Expand Down Expand Up @@ -205,7 +222,8 @@ struct CropImageView_Previews: PreviewProvider {
static var previews: some View {
PreviewView()
#if os(macOS)
.frame(minHeight: 750)
.frame(width: 500)
.frame(minHeight: 770)
#endif
}
}
10 changes: 8 additions & 2 deletions Sources/CropImage/UnderlyingImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct UnderlyingImageView: View {
@Binding var scale: CGFloat
@Binding var rotation: Angle
var image: PlatformImage
var initialImageSize: CGSize

@State private var tempOffset: CGSize = .zero
@State private var tempScale: CGFloat = 1
Expand All @@ -34,6 +35,10 @@ struct UnderlyingImageView: View {
var body: some View {
ZStack {
imageView
.resizable()
.scaledToFit()
.frame(width: initialImageSize.width, height: initialImageSize.height)
.animation(.default, value: initialImageSize)
.scaleEffect(scale * tempScale)
.offset(offset + tempOffset)
.rotationEffect(rotation + tempRotation)
Expand All @@ -54,7 +59,7 @@ struct UnderlyingImageView: View {
tempScale = value
}
.onEnded { value in
scale = scale * tempScale
scale = max(scale * tempScale, 0.01)
tempScale = 1
}
)
Expand Down Expand Up @@ -83,7 +88,8 @@ struct MoveAndScalableImageView_Previews: PreviewProvider {
offset: $offset,
scale: $scale,
rotation: $rotation,
image: .init(contentsOfFile: "/Users/laosb/Downloads/png.png")!
image: .init(contentsOfFile: "/Users/laosb/Downloads/png.png")!,
initialImageSize: .init(width: 200, height: 200)
)
}
}
Expand Down

0 comments on commit edc6a5c

Please sign in to comment.