Jump to content

[SWIFT] iOS Basic Camera Functionality Class


Swizzbeat

Recommended Posts

Little class I made since I needed basic Camera control in an iOS app I'm making. It may be useful to others as Swift is relatively new. Remember to create a delegate and set it to deal with responses when the user interacts with the camera view.

//  Copyright (c) 2014 Anthony Puppo. All rights reserved.

import Foundation
import UIKit

var cameraInstance:Camera!

func getCameraInstance() -> Camera {
    if (cameraInstance == nil) {
        cameraInstance = Camera()
    }
    return cameraInstance
}

class Camera {
    
    let cameraUI:UIImagePickerController!
    
    private init() {
        //Singleton Design Pattern
        if (isAvailable()) {
            self.cameraUI = UIImagePickerController()
        }
        self.cameraUI = nil
    }
    
    func isAvailable() -> Bool {
        return UIImagePickerController.isSourceTypeAvailable(.Camera)
    }
    
    func open(currentViewController:UIViewController) -> Bool {
        if (isAvailable() && cameraUI != nil) {
            currentViewController.presentViewController(cameraUI, animated: true, completion: nil)
            return true
        }
        return false
    }
    
    func setDelegate(delegate:protocol<UINavigationControllerDelegate, UIImagePickerControllerDelegate>) {
        if (cameraUI != nil) {
            cameraUI.delegate = delegate
        }
    }
    
    func setSourceType(sourceType:UIImagePickerControllerSourceType) {
        if (cameraUI != nil) {
            cameraUI.sourceType = sourceType
        }
    }
    
    func setMediaTypes(mediaTypes:AnyObject...) {
        if (cameraUI != nil) {
            cameraUI.mediaTypes = mediaTypes
        }
    }
    
    func setAllowsEditing(allowsEditing:Bool) {
        if (cameraUI != nil) {
            cameraUI.allowsEditing = allowsEditing
        }
    }
    
}
Edited by Swizzbeat
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...