Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

Commit

Permalink
updated to xcode8, swift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Milton Moura committed Oct 22, 2016
1 parent eb790df commit bfee401
Show file tree
Hide file tree
Showing 22 changed files with 252 additions and 201 deletions.
10 changes: 5 additions & 5 deletions CloudVisionKit.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Pod::Spec.new do |s|
s.name = "CloudVisionKit"
s.version = "0.1.0"
s.version = "0.3.0"
s.summary = "A Swift interface for Google's Cloud Vision API."

s.description = <<-DESC
Expand All @@ -18,15 +18,15 @@ Pod::Spec.new do |s|
s.source = { :git => "https://github.com/mgcm/CloudVisionKit.git", :tag => s.version.to_s }
s.social_media_url = 'https://twitter.com/mgcm'

s.ios.deployment_target = '8.3'
s.ios.deployment_target = '9.0'
s.requires_arc = true

s.source_files = 'Pod/Classes/**/*'
s.resource_bundles = {
'CloudVisionKit' => ['Pod/Assets/*.png']
}

s.dependency 'Alamofire', '~> 3.0'
s.dependency 'SwiftyJSON', '~> 2.3.1'
s.dependency 'Unbox', '~> 1.3'
s.dependency 'Alamofire', '~> 4.0.1'
s.dependency 'SwiftyJSON', '~> 3.1.1'
s.dependency 'Unbox', '~> 2.1'
end
127 changes: 70 additions & 57 deletions Example/CloudVisionKit.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
18 changes: 9 additions & 9 deletions Example/CloudVisionKit/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

let bundle = NSBundle.mainBundle()
let bundle = Bundle.main

guard let path = bundle.pathForResource("team", ofType: "jpg") else {
guard let path = bundle.path(forResource: "team", ofType: "jpg") else {
NSLog("Image not found.")
return true
}

let data = NSData(contentsOfFile: path)!
let data = try! Data(contentsOf: URL(fileURLWithPath: path))

let feature = GCVFeature(type: .Face, maxResults: 1000)
let request = GCVSingleRequest(image: data, features: [feature])
Expand All @@ -63,25 +63,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return true
}

func applicationWillResignActive(application: UIApplication) {
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Expand Down
12 changes: 6 additions & 6 deletions Example/Podfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '8.0'
platform :ios, '9.0'
use_frameworks!

pod 'Alamofire', '~> 3.0'
pod 'SwiftyJSON', '~> 2.3.1'
pod 'Unbox', '~> 1.3'
pod 'Alamofire', '~> 4.0.1'
pod 'SwiftyJSON', '~> 3.1.1'
pod 'Unbox', '~> 2.1'

plugin 'cocoapods-keys', {
:project => "CloudVisionKit",
Expand All @@ -16,10 +16,10 @@ plugin 'cocoapods-keys', {
]
}

target 'CloudVisionKit_Example', :exclusive => true do
target 'CloudVisionKit_Example' do
pod "CloudVisionKit", :path => "../"
end

target 'CloudVisionKit_Tests', :exclusive => true do
target 'CloudVisionKit_Tests' do
pod "CloudVisionKit", :path => "../"
end
36 changes: 19 additions & 17 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
PODS:
- Alamofire (3.2.0)
- CloudVisionKit (0.1.0):
- Alamofire (~> 3.0)
- SwiftyJSON (~> 2.3.1)
- Unbox (~> 1.3)
- Alamofire (4.0.1)
- CloudVisionKit (0.3.0):
- Alamofire (~> 4.0.1)
- SwiftyJSON (~> 3.1.1)
- Unbox (~> 2.1)
- Keys (1.0.0)
- SwiftyJSON (2.3.2)
- Unbox (1.3.1)
- SwiftyJSON (3.1.1)
- Unbox (2.1)

DEPENDENCIES:
- Alamofire (~> 3.0)
- Alamofire (~> 4.0.1)
- CloudVisionKit (from `../`)
- Keys (from `Pods/CocoaPodsKeys`)
- SwiftyJSON (~> 2.3.1)
- Unbox (~> 1.3)
- SwiftyJSON (~> 3.1.1)
- Unbox (~> 2.1)

EXTERNAL SOURCES:
CloudVisionKit:
:path: ../
:path: "../"
Keys:
:path: Pods/CocoaPodsKeys

SPEC CHECKSUMS:
Alamofire: 4da478599fccddff361205c8929333d7fe18dceb
CloudVisionKit: 99f2458e7290f6a63430be7e6e7e6b33219638c8
Keys: 7d2ff6ff42f6903358267ce56e9392f83c31acfe
SwiftyJSON: 04ccea08915aa0109039157c7974cf0298da292a
Unbox: b6e468b47b5d941b197c1ea53e9c179528b454ed
Alamofire: 7682d43245de14874acd142ec137b144aa1dd335
CloudVisionKit: e4a847a8453042bc106eca1c6648b4d75ec5995e
Keys: 9c35bf00f612ee1d48556f4a4b9b4551e224e90f
SwiftyJSON: f0be2e604f83e8405a624e9f891898bf6ed4e019
Unbox: 7070b89261391fbd1a9ccb5029079a884b897a62

COCOAPODS: 0.39.0
PODFILE CHECKSUM: d10a7b61b1f09d3cb87d85564d832bfa9b68971f

COCOAPODS: 1.1.1
2 changes: 1 addition & 1 deletion Example/Tests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Tests: XCTestCase {

func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock() {
self.measure() {
// Put the code you want to measure the time of here.
}
}
Expand Down
30 changes: 25 additions & 5 deletions Pod/Classes/CloudVisionAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,36 @@
import Foundation
import SwiftyJSON
import Alamofire
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l < r
case (nil, _?):
return true
default:
return false
}
}

fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l > r
default:
return rhs < lhs
}
}


final public class CloudVision {

private var apiKey: String?
fileprivate var apiKey: String?

public init(apiKey: String) {
self.apiKey = apiKey
}

public func annotateImage(request: GCVRequest, closure: (GCVResponse) -> Void) {
public func annotateImage(_ request: GCVRequest, closure: @escaping (GCVResponse) -> Void) {
let apiManager = GCVApiManager(apiKey: self.apiKey!)

do {
Expand All @@ -47,11 +67,11 @@ final public class CloudVision {
}
closure(response)
})
} catch GCVApiError.RequestSizeExceeded {
} catch GCVApiError.requestSizeExceeded {
NSLog("GCV Usage Limit Exceeded: Request Size > 8 MB")
} catch GCVApiError.ImageDataSizeExceeded {
} catch GCVApiError.imageDataSizeExceeded {
NSLog("GCV Usage Limit Exceeded: Image size > 2 MB")
} catch GCVApiError.ImagesPerRequestExceeded {
} catch GCVApiError.imagesPerRequestExceeded {
NSLog("GCV Usage Limit Exceeded: Max Images Per Request > 16")
} catch {
NSLog("GCV Error: Request Failed")
Expand Down
12 changes: 6 additions & 6 deletions Pod/Classes/GCVAnnotateImageResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public struct GCVAnnotateImageResponse: Unboxable {
public var textAnnotations: [GCVEntityAnnotation]?

public init(unboxer: Unboxer) {
self.faceAnnotations = unboxer.unbox("faceAnnotations")
self.landmarkAnnotations = unboxer.unbox("landmarkAnnotations")
self.logoAnnotations = unboxer.unbox("logoAnnotations")
self.labelAnnotations = unboxer.unbox("labelAnnotations")
self.textAnnotations = unboxer.unbox("textAnnotations")
self.faceAnnotations = unboxer.unbox(key: "faceAnnotations")
self.landmarkAnnotations = unboxer.unbox(key: "landmarkAnnotations")
self.logoAnnotations = unboxer.unbox(key: "logoAnnotations")
self.labelAnnotations = unboxer.unbox(key: "labelAnnotations")
self.textAnnotations = unboxer.unbox(key: "textAnnotations")
}
}
}
12 changes: 6 additions & 6 deletions Pod/Classes/GCVApiError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

public enum GCVApiError : ErrorType {
case InvalidCredentials
case RequestSizeExceeded
case ImageDataSizeExceeded
case ImagesPerRequestExceeded
}
public enum GCVApiError : Error {
case invalidCredentials
case requestSizeExceeded
case imageDataSizeExceeded
case imagesPerRequestExceeded
}
35 changes: 18 additions & 17 deletions Pod/Classes/GCVApiManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,44 @@ import Unbox

internal class GCVApiManager {

private static let apiVersion = "v1"
private static let baseURL = "https://vision.googleapis.com/" + GCVApiManager.apiVersion + "/images:annotate"
private var apiKey: String?
fileprivate static let apiVersion = "v1"
fileprivate static let baseURL = "https://vision.googleapis.com/" + GCVApiManager.apiVersion + "/images:annotate"
fileprivate var apiKey: String?

init(apiKey: String) {
self.apiKey = apiKey
}

private func buildQueryURL() -> NSURL? {
if let components = NSURLComponents(string: GCVApiManager.baseURL) {
components.queryItems = [NSURLQueryItem(name: "key", value: self.apiKey)]
return components.URL
fileprivate func buildQueryURL() -> URL? {
if var components = URLComponents(string: GCVApiManager.baseURL) {
components.queryItems = [URLQueryItem(name: "key", value: self.apiKey)]
return components.url
}
return nil
}

private func requestHeaders() -> [String: String] {
fileprivate func requestHeaders() -> [String: String] {
return ["Content-Type": "application/json"];
}

internal func performRequest(payload: [String: AnyObject]?, closure: (GCVResponse) -> Void) throws -> Void {
let request = Alamofire.request(.POST, self.buildQueryURL()!, parameters: payload, encoding: .JSON, headers: self.requestHeaders())
internal func performRequest(_ payload: [String: AnyObject]?, closure: @escaping (GCVResponse) -> Void) throws -> Void {
//let request = Alamofire.request(.POST, self.buildQueryURL()!, parameters: payload, encoding: .JSON, headers: self.requestHeaders())
let request = Alamofire.request(self.buildQueryURL()!, method: .post, parameters: payload, encoding: JSONEncoding.default)

if let
urlRequest = request.request,
httpBody = urlRequest.HTTPBody {
if httpBody.length > 8000000 {
throw GCVApiError.RequestSizeExceeded
if let urlRequest = request.request {
if let httpBody = urlRequest.httpBody {
if httpBody.count > 8000000 {
throw GCVApiError.requestSizeExceeded
}
}
}

request.responseJSON { (response) -> Void in
if let JSON = response.result.value {
let resp = JSON as! Dictionary<String, AnyObject>
let r: GCVResponse = Unbox(resp)!
let r: GCVResponse = try! unbox(dictionary: resp)
closure(r)
}
}
}
}
}
28 changes: 24 additions & 4 deletions Pod/Classes/GCVBoundingPoly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,34 @@
// SOFTWARE.

import Unbox
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l < r
case (nil, _?):
return true
default:
return false
}
}

fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l > r
default:
return rhs < lhs
}
}


public struct GCVVertex: Unboxable {
public var x: Float?
public var y: Float?

public init(unboxer: Unboxer) {
self.x = unboxer.unbox("x")
self.y = unboxer.unbox("y")
self.x = unboxer.unbox(key: "x")
self.y = unboxer.unbox(key: "y")
}
}

Expand All @@ -41,7 +61,7 @@ public struct GCVBoundingPoly: Unboxable {
public var vertices: [GCVVertex]?

public init(unboxer: Unboxer) {
self.vertices = unboxer.unbox("vertices")
self.vertices = unboxer.unbox(key: "vertices")
}
}

Expand All @@ -57,4 +77,4 @@ extension GCVBoundingPoly: CustomStringConvertible {
}
return string
}
}
}
Loading

0 comments on commit bfee401

Please sign in to comment.