Skip to content

Commit

Permalink
Resize images to 768x768px in GenerativeAIMultimodalSample (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheard authored Jan 11, 2024
1 parent 5cf2aae commit 4ab8724
Showing 1 changed file with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import SwiftUI

@MainActor
class PhotoReasoningViewModel: ObservableObject {
// Maximum value for the larger of the two image dimensions (height and width) in pixels. This is
// being used to reduce the image size in bytes.
private static let largestImageDimension = 768.0

private var logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "generative-ai")

@Published
Expand Down Expand Up @@ -61,7 +65,22 @@ class PhotoReasoningViewModel: ObservableObject {
var images = [PartsRepresentable]()
for item in selectedItems {
if let data = try? await item.loadTransferable(type: Data.self) {
images.append(ModelContent.Part.png(data))
guard let image = UIImage(data: data) else {
logger.error("Failed to parse data as an image, skipping.")
continue
}
if image.size.fits(largestDimension: PhotoReasoningViewModel.largestImageDimension) {
images.append(image)
} else {
guard let resizedImage = image
.preparingThumbnail(of: image.size
.aspectFit(largestDimension: PhotoReasoningViewModel.largestImageDimension)) else {
logger.error("Failed to resize image: \(image)")
continue
}

images.append(resizedImage)
}
}
}

Expand All @@ -81,3 +100,20 @@ class PhotoReasoningViewModel: ObservableObject {
}
}
}

private extension CGSize {
func fits(largestDimension length: CGFloat) -> Bool {
return width <= length && height <= length
}

func aspectFit(largestDimension length: CGFloat) -> CGSize {
let aspectRatio = width / height
if width > height {
let width = min(self.width, length)
return CGSize(width: width, height: round(width / aspectRatio))
} else {
let height = min(self.height, length)
return CGSize(width: round(height * aspectRatio), height: height)
}
}
}

0 comments on commit 4ab8724

Please sign in to comment.