Created
May 4, 2024 05:22
-
-
Save haileyok/e9499ac2b442ecadbf6497f46f0aba19 to your computer and use it in GitHub Desktop.
UIImageView VisionKit Image Analyzer Crash Repro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import VisionKit | |
class ViewController: UIViewController { | |
var imageView: UIImageView? | |
@available(iOS 16.0, macCatalyst 17.0, *) | |
static let imageAnalyzer = ImageAnalyzer.isSupported ? ImageAnalyzer() : nil | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view.sizeToFit() | |
let image = createImage() | |
let imageView = UIImageView(image: image) | |
imageView.frame = CGRect( | |
x: 0, | |
y: 0, | |
width: self.view.bounds.width, | |
height: self.view.bounds.height | |
) | |
imageView.contentMode = .scaleAspectFit | |
self.view.addSubview(imageView) | |
let imageAnalysisInteraction = ImageAnalysisInteraction() | |
imageView.addInteraction(imageAnalysisInteraction) | |
self.analyzeImage() | |
self.imageView = imageView | |
} | |
func createImage() -> UIImage { | |
guard let image = UIImage(named: "repro-image") else { | |
fatalError("Error loading image") | |
} | |
return image | |
} | |
func analyzeImage() { | |
guard #available(iOS 16.0, *), ImageAnalyzer.isSupported else { | |
return | |
} | |
Task { | |
guard let imageAnalyzer = Self.imageAnalyzer, | |
let imageAnalysisInteraction = self.findImageAnalysisInteraction(), | |
let image = self.imageView?.image | |
else { | |
return | |
} | |
let configuration = ImageAnalyzer.Configuration([.text, .machineReadableCode]) | |
do { | |
let imageAnalysis = try await imageAnalyzer.analyze(image, configuration: configuration) | |
if image == self.imageView?.image { | |
imageAnalysisInteraction.analysis = imageAnalysis | |
imageAnalysisInteraction.preferredInteractionTypes = .textSelection | |
} | |
} catch { | |
fatalError("Error analyzing image") | |
} | |
} | |
} | |
@available(iOS 16.0, macCatalyst 17.0, *) | |
private func findImageAnalysisInteraction() -> ImageAnalysisInteraction? { | |
let interaction = self.imageView?.interactions.first { | |
return $0 is ImageAnalysisInteraction | |
} | |
return interaction as? ImageAnalysisInteraction | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the image that repros the crash. For the original download, please use the URL:
https://cdn.bsky.app/img/feed_fullsize/plain/did:plc:b3psimnw5xxta2n4h5qzxu55/bafkreietuymak7njxkkr35ecjwlkhk3risz3zmfmvwak5hlji7agvdhcwm@jpeg
Steps to run:
repro-image