Created
July 23, 2023 17:19
-
-
Save swiftcafex/baafaf326d53e0192e42a1e09a44bd9c to your computer and use it in GitHub Desktop.
VisionKit 拖放图片示例
This file contains 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? | |
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