I am designing a photo editing application. Is it possible to automatically import the x, y, width, height values of these album frames into the application, when the user adds the photo album frame from the photos application? So can we automatically detect photo frames?. Because I will add the feature of adding photos to the frame by putting the plus(+) button in these frames. I need rects of all frames for now.
I share frames that can be an example:
I wrote code to find rectangular and transparent area, but it is not enough to find their position. Now, I'm sharing them:
func isAnyPixelTransparent(image: UIImage) -> Bool {
guard let cgImage = image.cgImage else {
return false // UIImage could not be converted to CGImage
}
let width = cgImage.width
let height = cgImage.height
// Create a buffer to hold pixel data
let pixelData = UnsafeMutablePointer<UInt8>.allocate(capacity: width * height * 4)
// Create a context to extract pixel data
let colorSpace = CGColorSpaceCreateDeviceRGB()
guard let context = CGContext(data: pixelData,
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: width * 4,
space: colorSpace,
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Big.rawValue) else {
return false // CGContext could not be created
}
// Draw the image into the context
context.draw(cgImage, in: CGRect(x: 0, y: 0, width: width, height: height))
// Loop through pixel data to check transparency
var isAnyPixelTransparent = false
for i in stride(from: 0, to: width * height * 4, by: 4) {
if pixelData[i + 3] == 0 {
isAnyPixelTransparent = true
break
}
}
// Clean up
pixelData.deallocate()
return isAnyPixelTransparent
}
func detectRectangles(in image: UIImage) -> Bool {
guard let cgImage = image.cgImage else {
print("Could not get CGImage from the provided image.")
return false
}
var rectanglesDetected = false
let request = VNDetectRectanglesRequest { request, error in
guard let observations = request.results as? [VNRectangleObservation] else {
print("No rectangle observations found.")
return
}
for observation in observations {
let rect = observation.boundingBox
print("Detected rectangle at \(rect)")
rectanglesDetected = true
}
}
let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
do {
try handler.perform([request])
} catch {
print("Error performing rectangle detection: \(error)")
rectanglesDetected = false
}
return rectanglesDetected
}


