Skip to content

Commit

Permalink
Fix crop restoration (#242)
Browse files Browse the repository at this point in the history
comparing cropped size in the actual image to the aspect ratio for
cropping UI will be different, as using floating values.
  • Loading branch information
muukii authored May 9, 2024
1 parent 3086e76 commit e3a23df
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Sources/BrightroomEngine/Core/EditingCrop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public struct EditingCrop: Equatable {
/// The angle that specifies rotation for the image.
public var rotation: Rotation = .angle_0

public private(set) var _usedAspectRatio: PixelAspectRatio?

/// An angle to rotate in addition to the specified rotation.
public var adjustmentAngle: AdjustmentAngle = .zero

Expand Down Expand Up @@ -177,6 +179,8 @@ public struct EditingCrop: Equatable {
size: maxSize
)

self._usedAspectRatio = newAspectRatio

self.cropExtent = Self.fittingRect(
rect: proposed,
in: imageSize,
Expand All @@ -199,12 +203,16 @@ public struct EditingCrop: Equatable {
- calculated fitting image size with ratio: (1745.0, 0.0, 4373.0, 5247.0)
- (4373.0 : 5247.0) -> 0.8334286259
*/
guard PixelAspectRatio(cropExtent.size) != newAspectRatio else {
guard _usedAspectRatio != newAspectRatio else {
return
}
updateCropExtent(toFitAspectRatio: newAspectRatio)
}

public mutating func purgeAspectRatio() {
_usedAspectRatio = nil
}

/**
Updates the crop extent to fit bounding box that comes from Vision.framework.
*/
Expand Down
2 changes: 2 additions & 0 deletions Sources/BrightroomUI/Shared/Components/Crop/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,8 @@ public final class CropView: UIView, UIScrollViewDelegate {
$0.preferredAspectRatio = ratio
if let ratio = ratio {
$0.proposedCrop?.updateCropExtentIfNeeded(toFitAspectRatio: ratio)
} else {
$0.proposedCrop?.purgeAspectRatio()
}
$0.layoutVersion += 1
}
Expand Down

0 comments on commit e3a23df

Please sign in to comment.