Skip to content

Commit

Permalink
Cache CroppedImageViews in ImagePreviewView for Improved Performance (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shima11 authored Apr 11, 2024
1 parent 189f94e commit 4740ce4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
15 changes: 7 additions & 8 deletions Sources/BrightroomEngine/Core/EditingStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -409,24 +409,23 @@ open class EditingStack: Hashable, StoreComponentType {
For previewing image
*/
public func makeCroppedCIImage(loadedState: State.Loaded) -> CIImage {
public func makeCroppedCIImage(
sourceImage: CGImage,
crop: EditingCrop,
orientation: CGImagePropertyOrientation
) -> CIImage {

do {
let orientation = loadedState.metadata.orientation
let crop = loadedState.currentEdit.crop

// orientation is not respected
let image = loadedState.editingSourceCGImage

// orientation-respected
let imageSize = image.size
let imageSize = sourceImage.size
.applying(cgOrientation: orientation)

let scaledCrop = crop.scaledWithPixelPerfect(
maxPixelSize: max(imageSize.width, imageSize.height)
)

return try image
return try sourceImage
// TODO: better to combine these operations - oriented and cropping
.oriented(orientation)
.croppedWithColorspace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public final class ImagePreviewView: PixelEditorCodeBasedView {
private weak var currentLoadingOverlay: UIView?

private var isBinding = false
private var cachedCroppedImage: (state: EditingStack.State.Loaded, image: CIImage)? = nil

// MARK: - Initializers

Expand Down Expand Up @@ -123,10 +124,27 @@ public final class ImagePreviewView: PixelEditorCodeBasedView {
}

private func requestPreviewImage(state: EditingStack.State.Loaded) {
let croppedImage = editingStack.makeCroppedCIImage(loadedState: state)

let croppedImage: CIImage
if
let cachedCroppedImage,
state.editingSourceCGImage == cachedCroppedImage.state.editingSourceCGImage,
state.metadata == cachedCroppedImage.state.metadata,
state.currentEdit.crop == cachedCroppedImage.state.currentEdit.crop
{
croppedImage = cachedCroppedImage.image
} else {
croppedImage = editingStack.makeCroppedCIImage(
sourceImage: state.editingSourceCGImage,
crop: state.currentEdit.crop,
orientation: state.metadata.orientation
)
cachedCroppedImage = (state, croppedImage)
}
imageView.display(image: croppedImage)
imageView.postProcessing = state.currentEdit.filters.apply
originalImageView.display(image: croppedImage)

}

private func updateLoadingOverlay(displays: Bool) {
Expand Down

0 comments on commit 4740ce4

Please sign in to comment.