Skip to content

Commit

Permalink
Improved Text Detector sample as use subFeatures property
Browse files Browse the repository at this point in the history
  • Loading branch information
shu223 committed Aug 30, 2015
1 parent e27dfa6 commit 10c204d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
4 changes: 2 additions & 2 deletions iOS9Sampler/Base.lproj/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8173.3" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8187.4" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8142"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8151.3"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
</dependencies>
<scenes>
Expand Down
26 changes: 24 additions & 2 deletions iOS9Sampler/SampleViewControllers/TextDetect.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8121.17" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="nBN-ic-paI">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8187.4" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="nBN-ic-paI">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8101.14"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8151.3"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
</dependencies>
<scenes>
<!--Text Detect View Controller-->
Expand Down Expand Up @@ -37,12 +38,33 @@
<constraint firstAttribute="trailing" secondItem="y94-P2-fBZ" secondAttribute="trailing" constant="8" id="el3-xd-0zv"/>
</constraints>
</scrollView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ddH-X9-xHh">
<rect key="frame" x="20" y="524" width="560" height="60"/>
<color key="backgroundColor" red="0.20000000300000001" green="0.60000002379999995" blue="0.80000001190000003" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstAttribute="height" constant="60" id="KXc-6l-Cda"/>
</constraints>
<state key="normal" title="DETECT">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
<integer key="value" value="3"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
<connections>
<action selector="detactBtnTapped:" destination="nBN-ic-paI" eventType="touchUpInside" id="zF9-tL-n67"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="ddH-X9-xHh" firstAttribute="leading" secondItem="dFH-KY-yh8" secondAttribute="leadingMargin" id="7LE-ZJ-FqJ"/>
<constraint firstItem="Ea1-Jg-FuG" firstAttribute="leading" secondItem="dFH-KY-yh8" secondAttribute="leading" id="DoY-Qa-Omg"/>
<constraint firstItem="YVR-pG-3Rg" firstAttribute="top" secondItem="Ea1-Jg-FuG" secondAttribute="bottom" id="FI7-pv-oLz"/>
<constraint firstAttribute="trailing" secondItem="Ea1-Jg-FuG" secondAttribute="trailing" id="H7T-wK-az4"/>
<constraint firstAttribute="trailingMargin" secondItem="ddH-X9-xHh" secondAttribute="trailing" id="bNU-YJ-ehS"/>
<constraint firstItem="YVR-pG-3Rg" firstAttribute="top" secondItem="ddH-X9-xHh" secondAttribute="bottom" constant="16" id="fCY-Zu-96f"/>
<constraint firstItem="Ea1-Jg-FuG" firstAttribute="top" secondItem="dFH-KY-yh8" secondAttribute="top" id="o4B-Le-0KA"/>
</constraints>
</view>
Expand Down
47 changes: 38 additions & 9 deletions iOS9Sampler/SampleViewControllers/TextDetectViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ class TextDetectViewController: UIViewController {

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

self.detect(imageView1)
self.detect(imageView2)
}

override func didReceiveMemoryWarning() {
Expand All @@ -42,6 +39,15 @@ class TextDetectViewController: UIViewController {
}
*/

private func rectForFeature(feature: CITextFeature, bounds: CGRect, scale: CGFloat) -> CGRect {

return CGRectMake(
feature.topLeft.x * scale,
bounds.height - feature.topLeft.y * scale,
feature.bounds.size.width * scale,
feature.bounds.size.height * scale)
}

private func detect(imageView: UIImageView) {

let image = CIImage(CGImage: imageView.image!.CGImage!)
Expand All @@ -51,22 +57,45 @@ class TextDetectViewController: UIViewController {
context: nil,
options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])

let features = detector.featuresInImage(image)
let features = detector.featuresInImage(
image,
options: [CIDetectorReturnSubFeatures: NSNumber(bool: true)])
let scale = imageView.frame.size.width / image.extent.width

for feature in features as! [CITextFeature] {
print("bounds:\(feature.bounds), topLeft:\(feature.topLeft), bottomRight:\(feature.bottomRight)")

let featureRect = CGRectMake(
feature.topLeft.x * scale,
imageView.bounds.height - feature.topLeft.y * scale,
feature.bounds.size.width * scale,
feature.bounds.size.height * scale)
// draw feature rects
let featureRect = self.rectForFeature(feature, bounds: imageView.bounds, scale: scale)
let featureView = UIView(frame: featureRect)
featureView.backgroundColor = UIColor.clearColor()
featureView.layer.borderColor = UIColor.greenColor().colorWithAlphaComponent(0.8).CGColor
featureView.layer.borderWidth = 2.0
imageView.addSubview(featureView)

// draw subFeature rects
for item in feature.subFeatures {

let subFeature = item as! CITextFeature
let subFeatureRect = self.rectForFeature(subFeature, bounds: imageView.bounds, scale: scale)
let subFeatureView = UIView(frame: subFeatureRect)
subFeatureView.backgroundColor = UIColor.clearColor()
subFeatureView.layer.borderColor = UIColor.yellowColor().colorWithAlphaComponent(0.8).CGColor
subFeatureView.layer.borderWidth = 1.0
imageView.addSubview(subFeatureView)
}
}
}


// =========================================================================
// MARK: - Actions

@IBAction func detactBtnTapped(sender: UIButton) {

sender.hidden = true

self.detect(imageView1)
self.detect(imageView2)
}
}

0 comments on commit 10c204d

Please sign in to comment.