-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy FixedSize to DynamicSize folder
- Loading branch information
Showing
6 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import SwiftUI | ||
|
||
extension DynamicSizeImageInfo { | ||
func toEditorInput() -> CropperEditorView.Input { | ||
return .init(uiimage: uiimage, scale: scale, offset: offset) | ||
} | ||
} | ||
|
||
struct DynamicSizeEditImageView: View { | ||
let info: DynamicSizeImageInfo | ||
@Binding var allImages: [DynamicSizeImageInfo] | ||
|
||
@Environment(\.dismiss) private var dismiss | ||
@State private var output = CropperEditorView.Output(scale: 1, offset: .zero) | ||
|
||
private var size: CGSize { DynamicSizeHomeView.cropSize } | ||
|
||
var body: some View { | ||
ZStack { | ||
CropperEditorView(input: info.toEditorInput(), output: $output) | ||
Circle().stroke() | ||
.frame(width: size.width, height: size.width) | ||
} | ||
.ignoresSafeArea() | ||
.navigationTitle("Edit Image") | ||
.navigationBarTitleDisplayMode(.inline) | ||
.toolbar { | ||
ToolbarItem(placement: .navigationBarTrailing) { | ||
Button("Save", action: save) | ||
} | ||
} | ||
} | ||
|
||
func save() { | ||
guard let index = allImages.firstIndex(where: { $0.id == info.id }) else { | ||
return | ||
} | ||
|
||
allImages[index].scale = output.scale | ||
allImages[index].offset = output.offset | ||
|
||
dismiss() | ||
} | ||
} | ||
|
||
#Preview { | ||
NavigationStack { | ||
DynamicSizeEditImageView( | ||
info: .init(uiimage: UIImage(named: "cachorro")!), | ||
allImages: .constant([]) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import SwiftUI | ||
|
||
struct DynamicSizeHomeView: View { | ||
static let cropSize = CGSize(width: 200, height: 200) | ||
|
||
private let columns = [GridItem(), GridItem(), GridItem()] | ||
private var size: CGSize { DynamicSizeHomeView.cropSize } | ||
|
||
@State private var imagesInfo = [ | ||
DynamicSizeImageInfo(uiimage: UIImage(named: "image-900-500")!, | ||
scale: nil, | ||
offset: nil) | ||
] | ||
|
||
var body: some View { | ||
NavigationStack { | ||
ScrollView { | ||
LazyVGrid(columns: columns, spacing: 40) { | ||
ForEach(imagesInfo) { info in | ||
let destination = DynamicSizeEditImageView(info: info, allImages: $imagesInfo) | ||
NavigationLink(destination: destination) { | ||
DynamicSizeImageVisualizerView(imageInfo: info) | ||
} | ||
} | ||
|
||
NavigationLink(destination: DynamicSizeNewImageView(allImages: $imagesInfo)) { | ||
buttonAdd | ||
} | ||
|
||
} | ||
} | ||
.navigationTitle("Fixed Size Example") | ||
} | ||
} | ||
|
||
var buttonAdd: some View { | ||
Circle().stroke().overlay { | ||
HStack { | ||
Image(systemName: "plus") | ||
Text("Add") | ||
} | ||
} | ||
.frame(width: size.width, height: size.height) | ||
} | ||
} | ||
|
||
#Preview { | ||
DynamicSizeHomeView() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import SwiftUI | ||
|
||
struct DynamicSizeImageInfo: Identifiable { | ||
let id = UUID() | ||
let uiimage: UIImage | ||
var scale: CGFloat? | ||
var offset: CGSize? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import SwiftUI | ||
|
||
struct DynamicSizeImageVisualizerView: View { | ||
let imageInfo: DynamicSizeImageInfo | ||
private var size: CGSize { DynamicSizeHomeView.cropSize } | ||
|
||
var body: some View { | ||
Image(uiImage: imageInfo.uiimage) | ||
.scaleEffect(imageInfo.scale ?? 1) | ||
.offset(imageInfo.offset ?? .zero) | ||
.frame(width: size.width, height: size.height) | ||
.clipShape(Circle()) | ||
.contentShape(Circle()) | ||
} | ||
} | ||
|
||
#Preview { | ||
DynamicSizeImageVisualizerView( | ||
imageInfo: .init(uiimage: UIImage(named:"image-900-500")!, | ||
scale: nil, offset: nil) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import SwiftUI | ||
|
||
struct DynamicSizeNewImageView: View { | ||
@Binding var allImages: [DynamicSizeImageInfo] | ||
|
||
@Environment(\.dismiss) private var dismiss | ||
@State private var imageName = "cachorro" | ||
@State private var output = CropperEditorView.Output(scale: 1, offset: .zero) | ||
|
||
private var size: CGSize { DynamicSizeHomeView.cropSize } | ||
private var uiimage: UIImage { UIImage(named: imageName)! } | ||
|
||
var body: some View { | ||
ZStack { | ||
CropperEditorView(input: .from(uiimage), output: $output) | ||
Circle().stroke() | ||
.frame(width: size.width, height: size.width) | ||
} | ||
.ignoresSafeArea() | ||
.navigationTitle("New Image") | ||
.navigationBarTitleDisplayMode(.inline) | ||
.toolbar { | ||
ToolbarItem(placement: .bottomBar) { | ||
Button("Change Image") { | ||
imageName = imageName == "cachorro" ? "image-900-500" : "cachorro" | ||
} | ||
} | ||
|
||
ToolbarItem(placement: .navigationBarTrailing) { | ||
Button("Save", action: save) | ||
} | ||
} | ||
} | ||
|
||
func save() { | ||
allImages.append(.init(uiimage: uiimage, | ||
scale: output.scale, | ||
offset: output.offset)) | ||
|
||
dismiss() | ||
} | ||
} | ||
#Preview { | ||
NavigationStack { | ||
DynamicSizeNewImageView(allImages: .constant([])) | ||
} | ||
} |