Created
August 12, 2017 13:28
-
-
Save Alicelovecode/0c67891ca5a03d8cb83fde02cc88149f to your computer and use it in GitHub Desktop.
seefood2.swift
This file contains hidden or 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 VisualRecognitionV3 | |
| class ViewController: UIViewController,UINavigationControllerDelegate,UIImagePickerControllerDelegate { | |
| let apiKey = "********************" | |
| //輸入你自己的IBMapiKey:長得像是f394cba206eb4ff8779fce96d95801f7494fdfc2的數字與英文字串 | |
| let version = "2017-08-12" | |
| @IBOutlet weak var imageView: UIImageView! | |
| @IBOutlet weak var cameraButton: UIBarButtonItem! | |
| let imagePicker = UIImagePickerController() | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| imagePicker.delegate = self | |
| } | |
| func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { | |
| if let image = info[UIImagePickerControllerOriginalImage] as? UIImage{ | |
| imageView.image = image | |
| imagePicker.dismiss(animated: true, completion: nil) | |
| let visualRecognition = VisualRecognition(apiKey: apiKey, version: version) | |
| let imageData = UIImageJPEGRepresentation(image, 0.01) | |
| let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! | |
| let fileURL = documentURL.appendingPathComponent("tempImage.jpg") | |
| try? imageData?.write(to: fileURL, options: []) | |
| visualRecognition.classify(imageFile: fileURL, success: { (classifiedImages) in | |
| print(classifiedImages) | |
| }) | |
| }else{ | |
| print("There was an error picking the image") | |
| } | |
| } | |
| @IBAction func cameraTapped(_ sender: UIBarButtonItem) { | |
| imagePicker.sourceType = .savedPhotosAlbum | |
| imagePicker.allowsEditing = false | |
| present(imagePicker, animated: true, completion: nil) | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment