Day 49: Stanford CS193p – Camera, Persistence, and Embed Segues

Day 49: Camera, Persistence, and Embed Segues

Screen Shot 2015-06-28 at 7.16.01 PM

Lecture 16

Camera- Add image to a waypoint
Persistence- Archiving, SQLite, File System, Core Data, demo on storing a waypoint image added by the user in the file system
Embed Segue- Putting an MVC’s View as a subview of another MVC’s View
In demo, show a “mini-map” of the waypoint when viewing its image


Modal View to get media from camera or photo library

Create it and set its delegate
Configure it
Present it
Respond to delegate methods when user is done/cancels picking media

Covered some stuff users can do depending on platform

Covered how not every source type can give video, and which ones can.

Went into how you can get even more out of the camera

Looked at some code to set source and media type in picker

var allowsEditing: Bool

Limiting Video Capture

Present the picker:
presentViewController(picker, animated: true, completion: nil)

Delegate will be notified when user is done:
func imagePickerController(UIIPC, didFinishPickingMediaWithInfo info: NSDictionary) {
//extract image/movie/metadata from info
presentingViewController.dismissViewControllerAnimated(true, completion: nil)

Also dismiss it when cancel happens:
func imagePickerControllerDidCancel(UIIPC) {
presentingViewController.dismissViewControllerAnimated(true, completion: nil)

Then we checked out what is in the info dictionary, …there’s a lot!

Save taken images or video to devices photo library:

Overlay View:
var cameraOverlayView: UIView
set view’s frame properly
camera is always full screen
but if use controls at bottom, might want view to be smaller

Hiding the normal camera controls (at bottom)
var showsCameraControls: Bool
leaves blank area on bottom
no controls, will need overlay view with “take picture” button
the button should send takePicture() to picker
then dismiss:
dismissModalViewController when done taking pictures

Can zoom or translate image while capturing:
var cameraViewTransform: CGAffineTransform

Archiving- rarely used for this
SQLite- rarely used unless you have a SQL database need to access
File Syste- iOS has a Uniz filesystem underneath it
Core Data- object-oriented database, primary way to store data in sophisticated app
hooks up easily to iCloud

machanism for making ANY object graph persistent
view hierarchies build in xCode:
obviously graphs of very complicated objects
Requires all objects in graph to implement NSCoding protocol:
func encodeWithCoder(encoder: NSCoder)
init(coder: NSCoder)

fast, low memory, reliable
open source, bundled in iOS
not good for everything (video, sounds, images)
not a server-based technology
used by Core Data

File System:
Accessing files in Unix filesystem:
Get the root of a path into an NSURL
Append path components to the URL
Write to/read from the files
Manage the filesystem with NSFileManager

Can only write inside your app’s “sandbox”

If user deletes your app, it deletes entire sandbox

What’s in that sandbox”
bundle directory
documents directory
Caches directory
among other directories: NSSearchPathDirectory for more.

How to get path to sandbox directories?

let fileManager = NSFileManager()
let urls: [NSURL] = fileManager.URLsForDirectory(NSSearchPathDirectory, inDomain: NSUserDomainMask)

examples of NSSearchPathDirectory values:
NSDocumentsDirectory, NSCachesDirectory, NSDocumentationDirectory, etc.

Getting URL, then…

Using it, by
reading/writing binary data to files

init?(contentsOfURL: NSURL)
func writeToURL(NSURL, atomically: Bool) -> Bool

provides utility operations
check t see if files exist; creates & enumerates directories; move, copy, dleete files
Thread safe


let manager = NSFileManagerr()
func createDictionaryAtURL(NSURL, withIntermediateDirectories: Bool, attritbutes: [NSObject: AnyObject]?, error: NSErrorPointer) -> Bool
func isReadableFileAtPath(String) -> Bool

also has delegate with lost of “should” methods

Where to store data?

Core Data:
large amounts of data or query in sophisticated manner

Enter Data Core:
object-oriented database
powerful framework

Way of creating an object graph backed by a database”
backed by SQL

How does it work?
create a visual mapping between database and objects
create and query for objects using object-oriented API
access the “columns in the database table” using
@NSManaged vars on objects

creating schema with visual map
in new file

create entites and attributes

get a UIManagedObjectContext:
either one click Use Core Data switch when create new Project
can use UIManagedDocument

with a context, you can create and query database objects:

func NSEntityDescription.insertNewObjectForEntityForName(String, inManagedObjectContext: UIManagedObjectContext) -> NSManagedObject

let fetchedObjects = managedObjectContext.executeFetchRequest(NSFetchRequest)

Setting attributes on objects from the database:
set/get values with:

func setValue(Object!, forKey: String)
func valueForKey(String) -> AnyObject!

or create subclass

class Photo: NSObject {
@NSManaged var title: String

and set and get property:

let photo = NSEntityDescription.insertNewObjectForEntityForName(“Photo”, inMain…)
photo.title = “My First Photo”

Table View and Core Data:
also Helper class for hooking up a Core Data database to a UITableView
it takes that and ensures the table is always showing the results of that request
The NSFetchedResultsController can implemetn all of your UITableViewDatasource methods

Embed Segues:

Putting a VC’s self.view in another VC’s view hierarchy

Xcode makes this easy:
drag out a Container View from object pallette into scene you want to embed it in
Automatically sets up “Embed Segue” from contrainer VC to contained VC

Embed Segue:
works like other segues
prepareForSegue(sender:), et. al.

View Loading Timing:
embedded VC’s outlets are not set at time prepareForSegue(sender:) is called

Demo time!

Showed how to do all the above and setting up an embedded segue view in the map application. Very cool stuff!

Screen Shot 2015-06-28 at 7.10.08 PM

Screen Shot 2015-06-28 at 7.15.17 PM


Published by: Travis Deaton

I studied International Relations at American University, volunteered for various organizations, and have recently returned from a 5 month tour of our National Parks. Currently an EMT/Wildland Fire & SAR student.

Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s