Skip to content

Commit

Permalink
Better example for Live Text
Browse files Browse the repository at this point in the history
  • Loading branch information
ba01ei committed Sep 23, 2022
1 parent b9337dc commit fa013b7
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Agrume/AgrumeCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,10 @@ extension AgrumeCell: UIScrollViewDelegate {

@available(iOS 16, *)
private func analyzeImage(_ image: UIImage) async {
guard ImageAnalyzer.isSupported else {
return
}

let analyzer = ImageAnalyzer()
let interaction = await MainActor.run {
let interaction = ImageAnalysisInteraction()
Expand Down
4 changes: 4 additions & 0 deletions Example/Agrume Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
39B9D7C228DE0B500016BE7F /* LiveTextViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39B9D7C128DE0B500016BE7F /* LiveTextViewController.swift */; };
39CA658926EFFC5700A5A910 /* URLUpdatedToImageViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39CA658826EFFC5700A5A910 /* URLUpdatedToImageViewController.swift */; };
771DA7342179EF1800541206 /* SwiftyGif.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 771DA7332179EF1800541206 /* SwiftyGif.framework */; };
9464AFE923C692C7006ADEBD /* OverlayView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9464AFE823C692C7006ADEBD /* OverlayView.swift */; };
Expand Down Expand Up @@ -80,6 +81,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
39B9D7C128DE0B500016BE7F /* LiveTextViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LiveTextViewController.swift; sourceTree = "<group>"; };
39CA658826EFFC5700A5A910 /* URLUpdatedToImageViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLUpdatedToImageViewController.swift; sourceTree = "<group>"; };
771DA7332179EF1800541206 /* SwiftyGif.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = SwiftyGif.framework; sourceTree = BUILT_PRODUCTS_DIR; };
9464AFE823C692C7006ADEBD /* OverlayView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OverlayView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -181,6 +183,7 @@
E77809E21D17821400CC60F1 /* SingleImageModalViewController.swift */,
F2D9598D1B1A133800073772 /* SingleImageViewController.swift */,
F2D959901B1A140200073772 /* SingleURLViewController.swift */,
39B9D7C128DE0B500016BE7F /* LiveTextViewController.swift */,
39CA658826EFFC5700A5A910 /* URLUpdatedToImageViewController.swift */,
F2A520181B130C7E00924912 /* Supporting Files */,
);
Expand Down Expand Up @@ -362,6 +365,7 @@
F2D9598E1B1A133800073772 /* SingleImageViewController.swift in Sources */,
F2539BD420F2418900062C80 /* CustomCloseButtonViewController.swift in Sources */,
F2A5201B1B130C7E00924912 /* AppDelegate.swift in Sources */,
39B9D7C228DE0B500016BE7F /* LiveTextViewController.swift in Sources */,
9464AFE923C692C7006ADEBD /* OverlayView.swift in Sources */,
F2D959971B1A199F00073772 /* MultipleURLsCollectionViewController.swift in Sources */,
F224A73227832DD900A8F5ED /* SwiftUIExampleViewController.swift in Sources */,
Expand Down
49 changes: 49 additions & 0 deletions Example/Agrume Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,26 @@
<segue destination="cCQ-GJ-dUD" kind="show" id="r46-tO-UZQ"/>
</connections>
</tableViewCell>
<tableViewCell contentMode="scaleToFill" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" textLabel="6Zp-bu-jIU" style="IBUITableViewCellStyleDefault" id="Hfv-Wz-qVQ">
<rect key="frame" x="0.0" y="578" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="Hfv-Wz-qVQ" id="KQk-qO-EJ6">
<rect key="frame" x="0.0" y="0.0" width="348.5" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="With Live Text" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="6Zp-bu-jIU">
<rect key="frame" x="16" y="0.0" width="324.5" height="44"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</tableViewCellContentView>
<connections>
<segue destination="qLg-q5-gAS" kind="show" id="5bd-8h-ouw"/>
</connections>
</tableViewCell>
</cells>
</tableViewSection>
</sections>
Expand Down Expand Up @@ -733,6 +753,35 @@
</objects>
<point key="canvasLocation" x="2001" y="2266"/>
</scene>
<!--Live Text Image View Controller-->
<scene sceneID="1WM-Xw-5br">
<objects>
<viewController title="Live Text Image View Controller" id="qLg-q5-gAS" customClass="LiveTextViewController" customModule="Agrume_Example" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="6HJ-JL-fid"/>
<viewControllerLayoutGuide type="bottom" id="hXH-Vp-sjT"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="Vfa-Ml-qsJ">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="dFR-Lm-f5v">
<rect key="frame" x="146" y="318" width="83" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Open Image"/>
<connections>
<action selector="openImage:" destination="qLg-q5-gAS" eventType="touchUpInside" id="gRd-G8-Zjs"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
</view>
<navigationItem key="navigationItem" id="Hrj-fJ-cCk"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="0eg-SZ-Rgn" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1281" y="2956"/>
</scene>
</scenes>
<resources>
<systemColor name="systemBackgroundColor">
Expand Down
18 changes: 9 additions & 9 deletions Example/Agrume Example/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
Expand All @@ -22,15 +27,6 @@
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Photos access is required to save photos in your library</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Photos access is required to save photos in your library</string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
Expand All @@ -45,5 +41,9 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Photos access is required to save photos in your library</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Photos access is required to save photos in your library</string>
</dict>
</plist>
26 changes: 26 additions & 0 deletions Example/Agrume Example/LiveTextViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// LiveTextViewController.swift
//

import Agrume
import UIKit
import VisionKit

final class LiveTextViewController: UIViewController {
@IBAction private func openImage(_ sender: Any) {
if #available(iOS 16, *) {
if ImageAnalyzer.isSupported {
let agrume = Agrume(
image: UIImage(named: "TextAndQR")!,
enableLiveText: true
)
agrume.show(from: self)
return
}
}

let alert = UIAlertController(title: "Not supported on this device", message: "Live Text is available for devices with iOS 16 (or above) and A12 (or above) Bionic chip (iPhone XS and later, physical device only)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel))
present(alert, animated: true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ final class MultipleImagesCollectionViewController: UICollectionViewController {

private let images = [
UIImage(named: "MapleBacon")!,
UIImage(named: "EvilBacon")!,
UIImage(named: "TextAndQR")!,
UIImage(named: "EvilBacon")!
]

override func viewDidLoad() {
Expand All @@ -36,7 +35,7 @@ final class MultipleImagesCollectionViewController: UICollectionViewController {
// MARK: UICollectionViewDelegate

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let agrume = Agrume(images: images, startIndex: indexPath.item, background: .blurred(.regular), enableLiveText: true)
let agrume = Agrume(images: images, startIndex: indexPath.item, background: .blurred(.regular))
agrume.didScroll = { [unowned self] index in
self.collectionView?.scrollToItem(at: IndexPath(item: index, section: 0), at: [], animated: false)
}
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ agrume.onLongPress = helper.makeSaveToLibraryLongPressGesture

You can customise the look and functionality of the image views. To do so, you need create a class that inherits from `AgrumeOverlayView: UIView`. As this is nothing more than a regular `UIView` you can do anything you want with it like add a custom toolbar or buttons to it. The example app shows a detailed example of how this can be achieved.

### Live Text Support

Agrume supports Live Text introduced since iOS 16. This allows user to interact with texts and QR codes in the image. It is available for iOS 16 or newer, on devices with A12 Bionic Chip (iPhone XS) or newer.

```swift
let agrume = Agrume(image: UIImage(named: "")!, enableLiveText: true)
```

### Lifecycle

`Agrume` offers the following lifecycle closures that you can optionally set:
Expand Down

0 comments on commit fa013b7

Please sign in to comment.