Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,12 @@ struct ContentView: View {
/// - Parameter uiImage: The input image that may have incorrect orientation metadata.
/// - Returns: A new UIImage with the correct orientation for processing.
func getCorrectOrientationUIImage(uiImage: UIImage) -> UIImage {
var newImage = UIImage()
let ciContext = CIContext()
switch uiImage.imageOrientation.rawValue {
case 1:
guard let orientedCIImage = CIImage(image: uiImage)?.oriented(CGImagePropertyOrientation.down),
let cgImage = ciContext.createCGImage(orientedCIImage, from: orientedCIImage.extent)
else { return uiImage }
guard uiImage.imageOrientation != .up else { return uiImage }

newImage = UIImage(cgImage: cgImage)
case 3:
guard let orientedCIImage = CIImage(image: uiImage)?.oriented(CGImagePropertyOrientation.right),
let cgImage = ciContext.createCGImage(orientedCIImage, from: orientedCIImage.extent)
else { return uiImage }
newImage = UIImage(cgImage: cgImage)
default:
newImage = uiImage
}
return newImage
UIGraphicsBeginImageContextWithOptions(uiImage.size, false, uiImage.scale)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 MEDIUM: Nice simplification! One small request: could we wrap the drawing block with defer { UIGraphicsEndImageContext() }? That guarantees the context is torn down even if we add another exit path or the draw routine starts returning early, and it keeps us safe from leaks if this helper evolves.

uiImage.draw(in: CGRect(origin: .zero, size: uiImage.size))
let normalizedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return normalizedImage ?? uiImage
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,38 @@ struct YOLOSingleImageSwiftUITests {
let correctedImage1 = getCorrectOrientationUIImage(uiImage: originalImage)
#expect(correctedImage1.size.width == originalImage.size.width)
#expect(correctedImage1.size.height == originalImage.size.height)
#expect(correctedImage1.imageOrientation == .up, "Normalized image should have .up orientation")

// Create image with orientation = .down (1)
let imageDown = UIImage(cgImage: originalImage.cgImage!, scale: 1.0, orientation: .down)
let correctedImageDown = getCorrectOrientationUIImage(uiImage: imageDown)

// Check if the function returns a valid image
#expect(correctedImageDown != nil, "Corrected image should not be nil")
#expect(
correctedImageDown.imageOrientation == .up, "Normalized image should have .up orientation")

// Create image with orientation = .left (3)
let imageLeft = UIImage(cgImage: originalImage.cgImage!, scale: 1.0, orientation: .left)
let correctedImageLeft = getCorrectOrientationUIImage(uiImage: imageLeft)

// Check if the function returns a valid image
#expect(correctedImageLeft != nil, "Corrected image should not be nil")

// For left orientation specifically, we'll verify the image data is handled correctly
// by checking the image isn't nil since we can't reliably check orientation property changes
#expect(
correctedImageLeft.imageOrientation == .up, "Normalized image should have .up orientation")
#expect(correctedImageLeft.size.width > 0, "Image width should be positive")
#expect(correctedImageLeft.size.height > 0, "Image height should be positive")

let orientations: [UIImage.Orientation] = [
.up, .down, .left, .right, .upMirrored, .downMirrored, .leftMirrored, .rightMirrored,
]
for orientation in orientations {
let testImage = UIImage(cgImage: originalImage.cgImage!, scale: 1.0, orientation: orientation)
let normalized = getCorrectOrientationUIImage(uiImage: testImage)
#expect(normalized.imageOrientation == .up, "All orientations should normalize to .up")
#expect(normalized.size.width > 0, "Normalized image should have valid width")
#expect(normalized.size.height > 0, "Normalized image should have valid height")
}
}

/// Tests the YOLO model initialization.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,14 @@ class ViewController: UIViewController, PHPickerViewControllerDelegate {
/// - Parameter uiImage: The input image that may have incorrect orientation metadata.
/// - Returns: A UIImage with the correct orientation for processing.
func getCorrectOrientationUIImage(uiImage: UIImage) -> UIImage {
var newImage = UIImage()
let ciContext = CIContext()
switch uiImage.imageOrientation.rawValue {
case 1:
guard
let orientedCIImage = CIImage(image: uiImage)?.oriented(CGImagePropertyOrientation.down),
let cgImage = ciContext.createCGImage(orientedCIImage, from: orientedCIImage.extent)
else { return uiImage }
guard uiImage.imageOrientation != .up else { return uiImage }

newImage = UIImage(cgImage: cgImage)
case 3:
guard
let orientedCIImage = CIImage(image: uiImage)?.oriented(CGImagePropertyOrientation.right),
let cgImage = ciContext.createCGImage(orientedCIImage, from: orientedCIImage.extent)
else { return uiImage }
newImage = UIImage(cgImage: cgImage)
default:
newImage = uiImage
}
return newImage
UIGraphicsBeginImageContextWithOptions(uiImage.size, false, uiImage.scale)
uiImage.draw(in: CGRect(origin: .zero, size: uiImage.size))
let normalizedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return normalizedImage ?? uiImage
}

/// Sets up the UI components including the image view and pick button.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,38 @@ struct YOLOSingleImageUIKitTests {
let correctedImage1 = viewController.getCorrectOrientationUIImage(uiImage: originalImage)
#expect(correctedImage1.size.width == originalImage.size.width)
#expect(correctedImage1.size.height == originalImage.size.height)
#expect(correctedImage1.imageOrientation == .up, "Normalized image should have .up orientation")

// Create image with orientation = .down (1)
let imageDown = UIImage(cgImage: originalImage.cgImage!, scale: 1.0, orientation: .down)
let correctedImageDown = viewController.getCorrectOrientationUIImage(uiImage: imageDown)

// Check if the function returns a valid image
#expect(correctedImageDown != nil, "Corrected image should not be nil")
#expect(
correctedImageDown.imageOrientation == .up, "Normalized image should have .up orientation")

// Create image with orientation = .left (3)
let imageLeft = UIImage(cgImage: originalImage.cgImage!, scale: 1.0, orientation: .left)
let correctedImageLeft = viewController.getCorrectOrientationUIImage(uiImage: imageLeft)

// Check if the function returns a valid image
#expect(correctedImageLeft != nil, "Corrected image should not be nil")

// For left orientation specifically, we'll verify the image data is handled correctly
// by checking the image isn't nil since we can't reliably check orientation property changes
#expect(
correctedImageLeft.imageOrientation == .up, "Normalized image should have .up orientation")
#expect(correctedImageLeft.size.width > 0, "Image width should be positive")
#expect(correctedImageLeft.size.height > 0, "Image height should be positive")

let orientations: [UIImage.Orientation] = [
.up, .down, .left, .right, .upMirrored, .downMirrored, .leftMirrored, .rightMirrored,
]
for orientation in orientations {
let testImage = UIImage(cgImage: originalImage.cgImage!, scale: 1.0, orientation: orientation)
let normalized = viewController.getCorrectOrientationUIImage(uiImage: testImage)
#expect(normalized.imageOrientation == .up, "All orientations should normalize to .up")
#expect(normalized.size.width > 0, "Normalized image should have valid width")
#expect(normalized.size.height > 0, "Normalized image should have valid height")
}
}

/// Tests the basic view controller properties without accessing private methods.
Expand Down