Skip to content

Instantly share code, notes, and snippets.

@haileyok
Created May 4, 2024 05:22
Show Gist options
  • Save haileyok/e9499ac2b442ecadbf6497f46f0aba19 to your computer and use it in GitHub Desktop.
Save haileyok/e9499ac2b442ecadbf6497f46f0aba19 to your computer and use it in GitHub Desktop.
UIImageView VisionKit Image Analyzer Crash Repro
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
}
}
@haileyok
Copy link
Author

haileyok commented May 4, 2024

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:

  • Create a new project using Storyboard and Swift
  • Add this image to the assets and name it repro-image
  • Run on a physical device

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment