Skip to content

Instantly share code, notes, and snippets.

@swiftcafex
Created July 23, 2023 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swiftcafex/baafaf326d53e0192e42a1e09a44bd9c to your computer and use it in GitHub Desktop.
Save swiftcafex/baafaf326d53e0192e42a1e09a44bd9c to your computer and use it in GitHub Desktop.
VisionKit 拖放图片示例
import UIKit
import VisionKit
class ViewController: UIViewController {
var imageView: UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
self.imageView = UIImageView(frame: CGRect.zero)
self.imageView?.image = UIImage(named: "cat")
self.view.addSubview(self.imageView!)
Task {
await self.initAnalysis()
}
}
func initAnalysis() async {
print("support \(ImageAnalyzer.isSupported)")
let interaction = ImageAnalysisInteraction()
self.imageView?.addInteraction(interaction)
let configuration = ImageAnalyzer.Configuration([.text, .machineReadableCode])
let analyzer = ImageAnalyzer()
let analysis = try? await analyzer.analyze(self.imageView!.image!, configuration: configuration)
interaction.analysis = analysis
interaction.preferredInteractionTypes = .imageSubject
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.imageView?.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.width)
self.imageView?.center = CGPoint(x: self.view.frame.size.width / 2, y: self.view.frame.size.height / 2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment