Created
July 26, 2023 12:31
-
-
Save swiftcafex/7236b9c9d47a1f831dc049994d6f08d5 to your computer and use it in GitHub Desktop.
VisionKit 的 Visual Look Up 模式
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: "cat2") | |
self.view.addSubview(self.imageView!) | |
Task { | |
await self.initAnalysis() | |
} | |
} | |
func initAnalysis() async { | |
let interaction = ImageAnalysisInteraction() | |
self.imageView?.addInteraction(interaction) | |
// 识别模式设置成 .visualLookUp | |
let configuration = ImageAnalyzer.Configuration([.visualLookUp]) | |
let analyzer = ImageAnalyzer() | |
let analysis = try? await analyzer.analyze(self.imageView!.image!, configuration: configuration) | |
interaction.analysis = analysis | |
// visualLookUp 模式, 通过点击图片上的识别按钮弹出内容 | |
// interaction.preferredInteractionTypes = .visualLookUp | |
// Subject lifting 模式,通过长按然后点击菜单弹出内容 | |
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