Skip to content

Commit

Permalink
Cap mask radius to image size
Browse files Browse the repository at this point in the history
  • Loading branch information
cvb941 committed May 2, 2024
1 parent edcad92 commit 2a996b8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Demo/SwiftyCropDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct ContentView: View {
.frame(maxWidth: .infinity, alignment: .leading)

Button {
maskRadius = UIScreen.main.bounds.width / 2
maskRadius = min(UIScreen.main.bounds.width, UIScreen.main.bounds.height) / 2
} label: {
Image(systemName: "arrow.up.left.and.arrow.down.right")
.font(.footnote)
Expand Down Expand Up @@ -135,7 +135,9 @@ struct ContentView: View {

// Example function for downloading an image
private func downloadExampleImage() async -> UIImage? {
let urlString = "https://picsum.photos/1000/1200"
let portraitUrlString = "https://picsum.photos/1000/1200"
let landscapeUrlString = "https://picsum.photos/2000/1000"
let urlString = Int.random(in: 0...1) == 0 ? portraitUrlString : landscapeUrlString
guard let url = URL(string: urlString),
let (data, _) = try? await URLSession.shared.data(from: url),
let image = UIImage(data: data)
Expand Down
8 changes: 6 additions & 2 deletions Sources/SwiftyCrop/Models/CropViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import UIKit

class CropViewModel: ObservableObject {
private let maxMagnificationScale: CGFloat
var imageSizeInView: CGSize = .zero
var maskRadius: CGFloat
var imageSizeInView: CGSize = .zero {
didSet {
maskRadius = min(maskRadius, min(imageSizeInView.width, imageSizeInView.height) / 2)
}
}
@Published var maskRadius: CGFloat

@Published var scale: CGFloat = 1.0
@Published var lastScale: CGFloat = 1.0
Expand Down

0 comments on commit 2a996b8

Please sign in to comment.