-
-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support iOS 16 Live Text #332
Conversation
921e40d
to
1f9d6fd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for opening the PR, this looks great and it's a nice addition to the library! I added some suggestions in the PR
Agrume/AgrumeCell.swift
Outdated
if enableLiveText { | ||
if #available(iOS 16, *) { | ||
if let image = image { | ||
Task { | ||
await analyzeImage(image) | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can simplify this expression and get rid of some of the nesting:
if enableLiveText { | |
if #available(iOS 16, *) { | |
if let image = image { | |
Task { | |
await analyzeImage(image) | |
} | |
} | |
} | |
} | |
if #available(iOS 16, *), enableLiveText, let image = image { | |
Task { | |
await analyzeImage(image) | |
} | |
} |
Agrume/AgrumeCell.swift
Outdated
private func analyzeImage(_ image: UIImage) async { | ||
guard ImageAnalyzer.isSupported else { | ||
return | ||
} | ||
|
||
let analyzer = ImageAnalyzer() | ||
let interaction = await MainActor.run { | ||
let interaction = ImageAnalysisInteraction() | ||
imageView.addInteraction(interaction) | ||
return interaction | ||
} | ||
let configuration = ImageAnalyzer.Configuration([.text, .machineReadableCode]) | ||
do { | ||
let analysis = try await analyzer.analyze(image, configuration: configuration) | ||
await MainActor.run { | ||
interaction.analysis = analysis | ||
interaction.preferredInteractionTypes = .automatic | ||
} | ||
} catch { | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we changed the method to not be async and instead encapsulate that inside of the method itself. Then we don't need the await MainActor.run
dance to construct the interaction. We could then also make use of Task { @MainActor in
Another alternative would be to keep the method async
but annotate it to run on the MainActor
:
@MainActor
private func analyzeImage(_ image: UIImage) async {
…
}
private func analyzeImage(_ image: UIImage) async { | |
guard ImageAnalyzer.isSupported else { | |
return | |
} | |
let analyzer = ImageAnalyzer() | |
let interaction = await MainActor.run { | |
let interaction = ImageAnalysisInteraction() | |
imageView.addInteraction(interaction) | |
return interaction | |
} | |
let configuration = ImageAnalyzer.Configuration([.text, .machineReadableCode]) | |
do { | |
let analysis = try await analyzer.analyze(image, configuration: configuration) | |
await MainActor.run { | |
interaction.analysis = analysis | |
interaction.preferredInteractionTypes = .automatic | |
} | |
} catch { | |
} | |
} | |
private func analyzeImage(_ image: UIImage) { | |
guard ImageAnalyzer.isSupported else { | |
return | |
} | |
let interaction = ImageAnalysisInteraction() | |
imageView.addInteraction(interaction) | |
let analyzer = ImageAnalyzer() | |
let configuration = ImageAnalyzer.Configuration([.text, .machineReadableCode]) | |
Task { @MainActor in | |
do { | |
let analysis = try await analyzer.analyze(image, configuration: configuration) | |
interaction.analysis = analysis | |
interaction.preferredInteractionTypes = .automatic | |
} catch { | |
print(error.localizedDescription) | |
} | |
} | |
} |
Thanks a lot for the feedback @JanGorman I have updated the code based on your suggestions. |
Awesome, thanks @ba01ei! |
Thanks a lot for merging @JanGorman. Do you plan to add a new release or tag some time soon? |
@ba01ei Sorry, didn't get around to it yesterday. I just published the new version: https://github.com/JanGorman/Agrume/releases/tag/5.8.5 |
@JanGorman @ba01ei did you test it? Seems the dependency was not added |
Interesting, it was working fine when I tried it |
This change allows Agrume to optionally enable iOS 16 Live Text.
Demo:
livetext2.mov