SlideShare a Scribd company logo
CS193p

Fall 2017-18
Stanford CS193p
Developing Applications for iOS

Fall 2017-18
CS193p

Fall 2017-18
Today
Core Motion

Detecting the position and movement of the device

Demo: Gravity-driven Playing Card

Camera

How to take pictures in your app

Demo: Taking a picture to be our background image in EmojiArt
CS193p

Fall 2017-18
Core Motion
API to access motion sensing hardware on your device

Primary inputs: Accelerometer, Gyro, Magnetometer

Not all devices have all inputs (e.g. only later model devices have a gyro)

Class used to get this input is CMMotionManager

Use only one instance per application (else performance hit)
It is a “global resource,” so getting one via a class method somewhere is okay

Usage

1. Check to see what hardware is available
2. Start the sampling going and poll the motion manager for the latest sample it has
... or ...
1. Check to see what hardware is available
2. Set the rate at which you want data to be reported from the hardware
3. Register a closure (and a queue to run it on) to call each time a sample is taken
CS193p

Fall 2017-18
Core Motion
Checking availability of hardware sensors

var {accelerometer,gyro,magnetometer,deviceMotion}Available: Bool
The “device motion” is a combination of all available (accelerometer, magnetometer, gyro).

We’ll talk more about that in a couple of slides.

Starting the hardware sensors collecting data

You only need to do this if you are going to poll for data.

Generally used when some architecture in your app is already periodic (e.g. animation frames).

func start{Accelerometer,Gyro,Magnetometer,DeviceMotion}Updates() 

Is the hardware currently collecting data?

var {accelerometer,gyro,magnetometer,deviceMotion}Active: Bool
Stop the hardware collecting data

It is a performance hit to be collecting data, so stop during times you don’t need the data.

func stop{Accelerometer,Gyro,Magnetometer,DeviceMotion}Updates()
CS193p

Fall 2017-18
Core Motion
Checking the data (from existing periodic mechanism)

var accelerometerData: CMAccelerometerData?
CMAccelerometerData object provides var acceleration: CMAcceleration
struct CMAcceleration {
var x: Double // in g (9.8 m/s/s)
var y: Double // in g
var z: Double // in g
}
This raw data includes acceleration due to gravity

So, if the device were laid flat, z would be 1.0 and x and y would be 0.0
CS193p

Fall 2017-18
Core Motion
Checking the data (from existing periodic mechanism)

var gyroData: CMGyroData?
CMGyroData object provides var rotationRate: CMRotationRate
struct CMRotationRate {
var x: Double // in radians/s
var y: Double // in radians/s
var z: Double // in radians/s
}
Sign of the rotation data follows right hand rule

The data above will be biased
CS193p

Fall 2017-18
Core Motion
Checking the data (from existing periodic mechanism)

var magnetometerData: CMMagnetometerData?
CMMagnetometerData object provides var magneticField: CMMagneticField
struct CMMagneticField {
var x: Double // in microteslas
var y: Double // in microteslas
var z: Double // in microteslas
}
The data above will be biased
CS193p

Fall 2017-18
CMDeviceMotion
CMDeviceMotion is a “combined” motion data source

It uses information from all the hardware to improve the data from each.

var deviceMotion: CMDeviceMotion? 

Acceleration Data in CMDeviceMotion

var gravity: CMAcceleration
var userAcceleration: CMAcceleration // gravity factored out using gyro

Other information in CMDeviceMotion

var rotationRate: CMRotationRate // bias removed from raw data using accelerometer
var attitude: CMAttitude // device’s attitude (orientation) in 3D space
class CMAttitude: NSObject // roll, pitch and yaw are in radians
var roll: Double // around longitudinal axis passing through top/bottom
var pitch: Double // around lateral axis passing through sides
var yaw: Double // around axis with origin at CofG and ⊥ to screen directed down
}
var heading: Double // in degrees, where 0 is north (true or magnetic depending on frame)
CS193p

Fall 2017-18
CMDeviceMotion
Reference Frame

Magnetometer use in CMDeviceMotion can be controlled by setting its reference frame.

Specify this when calling startDeviceMotionUpdates.

xArbitraryZVertical // the default, does not use magnetometer

xArbitraryCorrectedZVertical // uses magnetometer (if available) to correct yaw over time

xMagnetic/TrueNorthZVertical // uses magnetometer for device position/heading in world

These last two may require the user to calibrate the magnetometer.

And for TrueNorth, location information (e.g. GPS/Wifi/Cellular) will also be required.

North frames are necessary for apps that use things like Augmented Reality.

To get heading, for example, you must use a MagneticNorth or TrueNorth reference frame.

Always check to make sure the reference frame you want is available on the device …

static func availableAttitudeReferenceFrames() -> CMAttitudeReferenceFrame
CS193p

Fall 2017-18
Core Motion
Registering a block to receive Accelerometer data

func startAccelerometerUpdatesToQueue(queue: OperationQueue,
withHandler: CMAccelerometerHandler)
typealias CMAccelerationHandler = (CMAccelerometerData?, Error?) -> Void
queue can be an OperationQueue() you create or Operation.main (or current)

Registering a block to receive Gyro data

func startGyroUpdatesToQueue(queue: OperationQueue,
withHandler: CMGyroHandler)
typealias CMGyroHandler = (CMGyroData?, Error?) -> Void
Registering a block to receive Magnetometer data

func startMagnetometerUpdatesToQueue(queue: OperationQueue,
withHandler: CMMagnetometerHandler)
typealias CMMagnetometerHandler = (CMMagnetometerData?, Error?) -> Void
CS193p

Fall 2017-18
Core Motion
Registering a block to receive DeviceMotion data

func startDeviceMotionUpdates(using: CMAttitudeReferenceFrame,
queue: OperationQueue,
withHandler: (CMDeviceMotion?, Error?) -> Void)
queue can be an OperationQueue() you create or Operation.mainQueue (or currentQueue)

Errors … CMErrorDeviceRequiresMovement

CMErrorTrueNorthNotAvailable

CMErrorMotionActivityNotAvailable

CMErrorMotionActivityNotAuthorized
CS193p

Fall 2017-18
Core Motion
Setting the rate at which your block gets executed

var accelerometerUpdateInterval: TimeInterval
var gyroUpdateInterval: TimeInterval
var magnetometerUpdateInterval: TimeInterval
var deviceMotionUpdateInterval: TimeInterval
It is okay to add multiple handler blocks

Even though you are only allowed one CMMotionManager
However, each of the blocks will receive the data at the same rate (as set above)
(Multiple objects are allowed to poll at the same time as well, of course.)
CS193p

Fall 2017-18
Accelerometer Over Time
Historical Accelerometer Data

Sometimes you don’t need to look at the accelerometer in real time.

You just want to know what happened over a period of time in the past.

For example, if you want an idea of the user’s physical movement pattern.

The class CMSensorRecorder can record (at 50hz) and then play back accelerometer data.

Not all devices are capable of this (iPhone 7 and later, Apple Watch).

isAccelerometerRecordingAvailable() -> Bool // whether this device can record

Start recording data …

func recordAccelerometer(forDuration: TimeInterval) // keep this short for performance

Retrieving the recorded data …

func accelerometerData(from: Date, to: Date) -> CMSensorDataList // 3 day max

You enumerate over the CMAccelerometerData objects in a CMSensorDataList with for in …

for dataPoint: CMRecordedAccelerometerData in sensorDataList { . . . }
CS193p

Fall 2017-18
Activity Monitoring
Rough estimate of what the user is doing

For example, stationary, walking, running, automotive, or cycling.

You track this with a CMMotionActivityManager (not a CMMotionManager!).

func startActivityUpdates(to: OperationQueue, withHandler: (CMMotionActivity?) -> Void) 

CMMotionActivity is one of the above activities.

You can also query historical activity with …

func queryActivityStarting(from: Date, to: Date, to: OperationQueue, withHandler: …)
CS193p

Fall 2017-18
Pedometer
Pedometer

Getting the user’s “step” information only makes sense over time.

Create a CMPedometer and then send it the message …
func startUpdates(from: Date, withHandler: (CMPedometerData?, Error?) -> Void)
The from Date is allowed to be in the past (but only last 7 days are recorded).

Your handler will be called periodically with the struct CMPedometerData which has …

startDate and endDate of the data

numberOfSteps, distance, averageActivePace, and currentPace during the time

also floorsAscended and floorsDescended
Altimeter

Get relative altitude changes.

func startRelativeAltitudeUpdates(to: OperationQueue, withHandler: (CMAltitudeData?, Error?)) 

CMAltitudeData has both change in altitude in meters and raw atmospheric pressure data.
CS193p

Fall 2017-18
Core Motion
Checking the authorization status of hardware sensors

Some information is considered “private” to the user (e.g. fitness data).

Specifically CMPedometer, CMSensorRecorder, CMMotionActivityManager and CMAltimeter.

iOS will automatically ask the user (once) for permission to access this information.

You can find out what the status is at any time with this static func on each of these.

static func authorizationStatus() -> CMAuthorizationStatus 

struct CMAuthorizationStatus {
case notDetermined // user has not yet been asked
case restricted // fitness data access disabled in Settings
case denied // user has explicitly denied your app access
case authorized // ready to go!
} 

Lack of authorization may also show up as an error when you request data.
CS193p

Fall 2017-18
Demo
Playing Card

We’ll make our playing cards be affected by “real gravity” using the accelerometer.
CS193p

Fall 2017-18
UIImagePickerController
Modal view controller to get media from camera or photo library

i.e., you put it up with present(_:animated:completion:)

Usage

1. Create it & set its delegate (it can’t do anything without its delegate)
2. Configure it (source, kind of media, user edibility, etc.)
3. Present it
4. Respond to delegate methods when user is done/cancels picking the media

What the user can do depends on the platform

Almost all devices have cameras, but some can record video, some can not

You can only offer camera or photo library on iPad (not both together at the same time)
As with all device-dependent API, we want to start by check what’s available …
static func isSourceTypeAvailable(sourceType: UIImagePickerControllerSourceType) -> Bool
Source type is .photoLibrary or .camera or .savedPhotosAlbum (camera roll)
CS193p

Fall 2017-18
UIImagePickerController
But don’t forget that not every source type can give video

So, you then want to check ...
static func availableMediaTypes(for: UIImagePickerControllerSourceType) -> [String]?
Depending on device, will return one or more of these ...
kUTTypeImage // pretty much all sources provide this, hardly worth checking for even

kUTTypeLivePhoto // must also say kUTTypeImage for this one to work
kUTTypeMovie // audio and video together, only some sources provide this

You can get even more specific about cameras

(Though usually this is not necessary)
static func isCameraDeviceAvailable(UIImagePickerControllerCameraDevice) -> Bool
returns .rear or .front
There are other camera-specific interrogations too, for example …
static func isFlashAvailableForCameraDevice(UIImagePickerControllerCameraDevice) -> Bool
These are declared in the MobileCoreServices framework.

import MobileCoreServices
CS193p

Fall 2017-18
UIImagePickerController
Set the source and media type you want in the picker

Example setup of a picker for capturing video (kUTTypeMovie) …

(From here out, UIImagePickerController will be abbreviated UIIPC for space reasons.)
let picker = UIImagePickerController()
let mediaTypeMovie = kUTTypeMovie as String
picker.delegate = self // self must implement UINavigationControllerDelegate too
if UIIPC.isSourceTypeAvailable(.camera) {
picker.sourceType = .camera
if let availableTypes = UIIPC.availableMediaTypesForSourceType(.camera) {
if availableTypes.contains(mediaTypeMovie) {
picker.mediaTypes = [mediaTypeMovie]
// proceed to put the picker up
}
}
}
CS193p

Fall 2017-18
UIImagePickerController
Editability

var allowsEditing: Bool
If true, then the user will have opportunity to edit the image/video inside the picker.
When your delegate is notified that the user is done, you’ll get both raw and edited versions.

Limiting Video Capture

var videoQuality: UIImagePickerControllerQualityType
.typeMedium // default
.typeHigh
.type640x480
.typeLow
.typeIFrame1280x720 // native on some devices
.typeIFrame960x540 // native on some devices
var videoMaximumDuration: TimeInterval // defaults to 10 minutes
CS193p

Fall 2017-18
UIImagePickerController
Present the picker

present(picker, animated: true, completion: nil)
On iPad, if you are not offering Camera (just photo library), you must present with popover.

If you are offering the Camera on iPad, then full-screen is preferred.

Remember: on iPad, it’s Camera OR Photo Library (not both at the same time).

Delegate will be notified when user is done

func imagePickerController(UIIPC, didFinishPickingMediaWithInfo info: [String:Any]) {
// extract image/movie data/metadata here from info, more on the next slide
picker.presentingViewController?.dismiss(animated: true) { }
}
Also dismiss it when cancel happens

func imagePickerControllerDidCancel(UIIPC) {
picker.presentingViewController?.dismiss(animated: true) { }
}
CS193p

Fall 2017-18
UIImagePickerController
What is in that info dictionary?

UIImagePickerControllerMediaType // kUTTypeImage, kUTTypeMovie
UIImagePickerControllerOriginalImage // UIImage
UIImagePickerControllerEditedImage // UIImage
UIImagePickerControllerImageURL // URL (in a temp location, so move it to keep it)

UIImagePickerControllerCropRect // CGRect (in an NSValue)
UIImagePickerControllerMediaMetadata // Dictionary of info about the image

UIImagePickerControllerLivePhoto // a PHLivePhoto

UIImagePickerControllerPHAsset // a PHAsset (see PHPhotoLibrary)

UIImagePickerControllerMediaURL // URL of the video if kUTTypeMovie
CS193p

Fall 2017-18
UIImagePickerController
Saving taken images or video into the device’s photo library

You can save to the user’s Camera Roll … 

func UIImageWriteToSavedPhotosAlbum(
_ image: UIImage,
_ target: Any?, // the object to send selector to when finished writing
_ selector: Selector? // selector to send to target when finished writing
_ context: UnsafeMutableRawPointer? // passed to the selector
)
It’s a bummer that this isn’t closure-based, but it is what it is. 

This is a very simple and convenient way to do this.

But this only makes sense if the user only occasionally would want to save an image.

Otherwise, you’ll want to integrate with the Photos application: checkout PHPhotoLibrary.

Of course, you could also save the image into your own sandbox.

You’d do that if the captured images only make sense inside your own app.
CS193p

Fall 2017-18
UIImagePickerController
In general, much more sophisticated media capture is available

This UIImagePickerController API is pretty simple, but more powerful API exists.

Check out both PHPhotoLibrary and AVCaptureDevice.
CS193p

Fall 2017-18
UIImagePickerController
Overlay View

var cameraOverlayView: UIView
Be sure to set this view’s frame properly.
Camera is always full screen, so use UIScreen.main’s bounds property.
If you use the built-in controls at the bottom, you might want your view to be smaller.

Hiding the normal camera controls (at the bottom)

var showsCameraControls: Bool
Will leave a blank area at the bottom of the screen (camera’s aspect 4:3, not same as screen’s).
With no controls, you’ll need an overlay view with a “take picture” (at least) button.
That button should send takePicture() or (startVideoCapture()) to the picker.

Don’t forget to dismiss when you are done taking pictures.

You can zoom or translate the image while capturing

var cameraViewTransform: CGAffineTransform
For example, you might want to scale the image up to full screen (some of it will get clipped).
CS193p

Fall 2017-18
Core Image and Vision
Processing Images

Core Image is a powerful and efficient framework for applying filters to your images.

Has a couple of hundred filters to choose from (blur, depth, comparison, colors, smoothing, etc.).

Vision framework provides powerful feature detection in images (e.g. faces, barcodes, etc.).

Core Image also has some feature detection API.

Check out Core Image and Vision in the documentation.
CS193p

Fall 2017-18
Demo Code
Download the Emoji Art demo from today’s lecture.
Download the Playing Card demo from today’s lecture.

More Related Content

Similar to Camera And Email

Analytics with Spark
Analytics with SparkAnalytics with Spark
Analytics with Spark
Probst Ludwine
 
Samantha Wang [InfluxData] | Best Practices on How to Transform Your Data Usi...
Samantha Wang [InfluxData] | Best Practices on How to Transform Your Data Usi...Samantha Wang [InfluxData] | Best Practices on How to Transform Your Data Usi...
Samantha Wang [InfluxData] | Best Practices on How to Transform Your Data Usi...
InfluxData
 
W 0300 codingfor_life-batterylifethatis
W 0300 codingfor_life-batterylifethatisW 0300 codingfor_life-batterylifethatis
W 0300 codingfor_life-batterylifethatis
jicheng687
 
Design and implementation of GPS Tracker
Design and implementation of GPS TrackerDesign and implementation of GPS Tracker
Design and implementation of GPS Tracker
Vignesh Kannan
 
Virtual training Intro to Kapacitor
Virtual training  Intro to Kapacitor Virtual training  Intro to Kapacitor
Virtual training Intro to Kapacitor
InfluxData
 
VideoMR - A Map and Reduce Framework for Real-time Video Processing
VideoMR - A Map and Reduce Framework for Real-time Video ProcessingVideoMR - A Map and Reduce Framework for Real-time Video Processing
VideoMR - A Map and Reduce Framework for Real-time Video Processing
Matthias Trapp
 
NERC Presentation
NERC PresentationNERC Presentation
NERC Presentation
Drew Silcock
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisBeyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic Analysis
Fastly
 
Rapid Laser Scanning the process
Rapid Laser Scanning the processRapid Laser Scanning the process
Rapid Laser Scanning the process
Seeview Solutions
 
Developing AIR for Android with Flash Professional CS5
Developing AIR for Android with Flash Professional CS5Developing AIR for Android with Flash Professional CS5
Developing AIR for Android with Flash Professional CS5
Chris Griffith
 
Spark Summit EU talk by Francois Garillot and Mohamed Kafsi
Spark Summit EU talk by Francois Garillot and Mohamed KafsiSpark Summit EU talk by Francois Garillot and Mohamed Kafsi
Spark Summit EU talk by Francois Garillot and Mohamed Kafsi
Spark Summit
 
Mobility insights at Swisscom - Understanding collective mobility in Switzerland
Mobility insights at Swisscom - Understanding collective mobility in SwitzerlandMobility insights at Swisscom - Understanding collective mobility in Switzerland
Mobility insights at Swisscom - Understanding collective mobility in Switzerland
François Garillot
 
All projects
All projectsAll projects
All projects
Karishma Jain
 
Monitoring as Software Validation
Monitoring as Software ValidationMonitoring as Software Validation
Monitoring as Software Validation
BioDec
 
[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe
SAP HANA Cloud Platform
 
Analyzing Streaming Data in Real-time - AWS Summit Cape Town 2018
Analyzing Streaming Data in Real-time - AWS Summit Cape Town 2018Analyzing Streaming Data in Real-time - AWS Summit Cape Town 2018
Analyzing Streaming Data in Real-time - AWS Summit Cape Town 2018
Amazon Web Services
 
Urban flood prediction digital ocean august edition
Urban flood prediction   digital ocean august editionUrban flood prediction   digital ocean august edition
Urban flood prediction digital ocean august edition
transight
 
An Efficient System for Forward Collison Avoidance Using Low Cost Camera & Em...
An Efficient System for Forward Collison Avoidance Using Low Cost Camera & Em...An Efficient System for Forward Collison Avoidance Using Low Cost Camera & Em...
An Efficient System for Forward Collison Avoidance Using Low Cost Camera & Em...
aciijournal
 
The Wonderful-Amazing-Orientation-Motion-Sensormatic Machine
The Wonderful-Amazing-Orientation-Motion-Sensormatic MachineThe Wonderful-Amazing-Orientation-Motion-Sensormatic Machine
The Wonderful-Amazing-Orientation-Motion-Sensormatic Machine
Andrew Fisher
 
An Efficient System for Forward Collison Avoidance Using Low Cost Camera & Em...
An Efficient System for Forward Collison Avoidance Using Low Cost Camera & Em...An Efficient System for Forward Collison Avoidance Using Low Cost Camera & Em...
An Efficient System for Forward Collison Avoidance Using Low Cost Camera & Em...
aciijournal
 

Similar to Camera And Email (20)

Analytics with Spark
Analytics with SparkAnalytics with Spark
Analytics with Spark
 
Samantha Wang [InfluxData] | Best Practices on How to Transform Your Data Usi...
Samantha Wang [InfluxData] | Best Practices on How to Transform Your Data Usi...Samantha Wang [InfluxData] | Best Practices on How to Transform Your Data Usi...
Samantha Wang [InfluxData] | Best Practices on How to Transform Your Data Usi...
 
W 0300 codingfor_life-batterylifethatis
W 0300 codingfor_life-batterylifethatisW 0300 codingfor_life-batterylifethatis
W 0300 codingfor_life-batterylifethatis
 
Design and implementation of GPS Tracker
Design and implementation of GPS TrackerDesign and implementation of GPS Tracker
Design and implementation of GPS Tracker
 
Virtual training Intro to Kapacitor
Virtual training  Intro to Kapacitor Virtual training  Intro to Kapacitor
Virtual training Intro to Kapacitor
 
VideoMR - A Map and Reduce Framework for Real-time Video Processing
VideoMR - A Map and Reduce Framework for Real-time Video ProcessingVideoMR - A Map and Reduce Framework for Real-time Video Processing
VideoMR - A Map and Reduce Framework for Real-time Video Processing
 
NERC Presentation
NERC PresentationNERC Presentation
NERC Presentation
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisBeyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic Analysis
 
Rapid Laser Scanning the process
Rapid Laser Scanning the processRapid Laser Scanning the process
Rapid Laser Scanning the process
 
Developing AIR for Android with Flash Professional CS5
Developing AIR for Android with Flash Professional CS5Developing AIR for Android with Flash Professional CS5
Developing AIR for Android with Flash Professional CS5
 
Spark Summit EU talk by Francois Garillot and Mohamed Kafsi
Spark Summit EU talk by Francois Garillot and Mohamed KafsiSpark Summit EU talk by Francois Garillot and Mohamed Kafsi
Spark Summit EU talk by Francois Garillot and Mohamed Kafsi
 
Mobility insights at Swisscom - Understanding collective mobility in Switzerland
Mobility insights at Swisscom - Understanding collective mobility in SwitzerlandMobility insights at Swisscom - Understanding collective mobility in Switzerland
Mobility insights at Swisscom - Understanding collective mobility in Switzerland
 
All projects
All projectsAll projects
All projects
 
Monitoring as Software Validation
Monitoring as Software ValidationMonitoring as Software Validation
Monitoring as Software Validation
 
[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe
 
Analyzing Streaming Data in Real-time - AWS Summit Cape Town 2018
Analyzing Streaming Data in Real-time - AWS Summit Cape Town 2018Analyzing Streaming Data in Real-time - AWS Summit Cape Town 2018
Analyzing Streaming Data in Real-time - AWS Summit Cape Town 2018
 
Urban flood prediction digital ocean august edition
Urban flood prediction   digital ocean august editionUrban flood prediction   digital ocean august edition
Urban flood prediction digital ocean august edition
 
An Efficient System for Forward Collison Avoidance Using Low Cost Camera & Em...
An Efficient System for Forward Collison Avoidance Using Low Cost Camera & Em...An Efficient System for Forward Collison Avoidance Using Low Cost Camera & Em...
An Efficient System for Forward Collison Avoidance Using Low Cost Camera & Em...
 
The Wonderful-Amazing-Orientation-Motion-Sensormatic Machine
The Wonderful-Amazing-Orientation-Motion-Sensormatic MachineThe Wonderful-Amazing-Orientation-Motion-Sensormatic Machine
The Wonderful-Amazing-Orientation-Motion-Sensormatic Machine
 
An Efficient System for Forward Collison Avoidance Using Low Cost Camera & Em...
An Efficient System for Forward Collison Avoidance Using Low Cost Camera & Em...An Efficient System for Forward Collison Avoidance Using Low Cost Camera & Em...
An Efficient System for Forward Collison Avoidance Using Low Cost Camera & Em...
 

More from SV.CO

Handout level-1-module-1
Handout   level-1-module-1Handout   level-1-module-1
Handout level-1-module-1
SV.CO
 
Persistence And Documents
Persistence And DocumentsPersistence And Documents
Persistence And Documents
SV.CO
 
Building complex input screens
Building complex input screensBuilding complex input screens
Building complex input screens
SV.CO
 
Working with the Web: 
Decoding JSON
Working with the Web: 
Decoding JSONWorking with the Web: 
Decoding JSON
Working with the Web: 
Decoding JSON
SV.CO
 
Saving Data
Saving DataSaving Data
Saving Data
SV.CO
 
Alerts notification
Alerts notificationAlerts notification
Alerts notification
SV.CO
 
UI Dynamics
UI DynamicsUI Dynamics
UI Dynamics
SV.CO
 
Practical animation
Practical animationPractical animation
Practical animation
SV.CO
 
Segues and navigation controllers
Segues and navigation controllersSegues and navigation controllers
Segues and navigation controllers
SV.CO
 
Scroll views
Scroll viewsScroll views
Scroll views
SV.CO
 
Intermediate table views
Intermediate table viewsIntermediate table views
Intermediate table views
SV.CO
 
Table views
Table viewsTable views
Table views
SV.CO
 
Closures
ClosuresClosures
Closures
SV.CO
 
Protocols
ProtocolsProtocols
Protocols
SV.CO
 
App anatomy and life cycle
App anatomy and life cycleApp anatomy and life cycle
App anatomy and life cycle
SV.CO
 
Extensions
ExtensionsExtensions
Extensions
SV.CO
 
Gestures
GesturesGestures
Gestures
SV.CO
 
View controller life cycle
View controller life cycleView controller life cycle
View controller life cycle
SV.CO
 
Controls in action
Controls in actionControls in action
Controls in action
SV.CO
 
Auto layout and stack views
Auto layout and stack viewsAuto layout and stack views
Auto layout and stack views
SV.CO
 

More from SV.CO (20)

Handout level-1-module-1
Handout   level-1-module-1Handout   level-1-module-1
Handout level-1-module-1
 
Persistence And Documents
Persistence And DocumentsPersistence And Documents
Persistence And Documents
 
Building complex input screens
Building complex input screensBuilding complex input screens
Building complex input screens
 
Working with the Web: 
Decoding JSON
Working with the Web: 
Decoding JSONWorking with the Web: 
Decoding JSON
Working with the Web: 
Decoding JSON
 
Saving Data
Saving DataSaving Data
Saving Data
 
Alerts notification
Alerts notificationAlerts notification
Alerts notification
 
UI Dynamics
UI DynamicsUI Dynamics
UI Dynamics
 
Practical animation
Practical animationPractical animation
Practical animation
 
Segues and navigation controllers
Segues and navigation controllersSegues and navigation controllers
Segues and navigation controllers
 
Scroll views
Scroll viewsScroll views
Scroll views
 
Intermediate table views
Intermediate table viewsIntermediate table views
Intermediate table views
 
Table views
Table viewsTable views
Table views
 
Closures
ClosuresClosures
Closures
 
Protocols
ProtocolsProtocols
Protocols
 
App anatomy and life cycle
App anatomy and life cycleApp anatomy and life cycle
App anatomy and life cycle
 
Extensions
ExtensionsExtensions
Extensions
 
Gestures
GesturesGestures
Gestures
 
View controller life cycle
View controller life cycleView controller life cycle
View controller life cycle
 
Controls in action
Controls in actionControls in action
Controls in action
 
Auto layout and stack views
Auto layout and stack viewsAuto layout and stack views
Auto layout and stack views
 

Recently uploaded

Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
S. Raj Kumar
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
spdendr
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
BoudhayanBhattachari
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
ssuser13ffe4
 

Recently uploaded (20)

Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
 

Camera And Email

  • 1. CS193p Fall 2017-18 Stanford CS193p Developing Applications for iOS Fall 2017-18
  • 2. CS193p Fall 2017-18 Today Core Motion Detecting the position and movement of the device Demo: Gravity-driven Playing Card Camera How to take pictures in your app Demo: Taking a picture to be our background image in EmojiArt
  • 3. CS193p Fall 2017-18 Core Motion API to access motion sensing hardware on your device Primary inputs: Accelerometer, Gyro, Magnetometer Not all devices have all inputs (e.g. only later model devices have a gyro) Class used to get this input is CMMotionManager Use only one instance per application (else performance hit) It is a “global resource,” so getting one via a class method somewhere is okay Usage 1. Check to see what hardware is available 2. Start the sampling going and poll the motion manager for the latest sample it has ... or ... 1. Check to see what hardware is available 2. Set the rate at which you want data to be reported from the hardware 3. Register a closure (and a queue to run it on) to call each time a sample is taken
  • 4. CS193p Fall 2017-18 Core Motion Checking availability of hardware sensors var {accelerometer,gyro,magnetometer,deviceMotion}Available: Bool The “device motion” is a combination of all available (accelerometer, magnetometer, gyro). We’ll talk more about that in a couple of slides. Starting the hardware sensors collecting data You only need to do this if you are going to poll for data. Generally used when some architecture in your app is already periodic (e.g. animation frames). func start{Accelerometer,Gyro,Magnetometer,DeviceMotion}Updates() Is the hardware currently collecting data? var {accelerometer,gyro,magnetometer,deviceMotion}Active: Bool Stop the hardware collecting data It is a performance hit to be collecting data, so stop during times you don’t need the data. func stop{Accelerometer,Gyro,Magnetometer,DeviceMotion}Updates()
  • 5. CS193p Fall 2017-18 Core Motion Checking the data (from existing periodic mechanism) var accelerometerData: CMAccelerometerData? CMAccelerometerData object provides var acceleration: CMAcceleration struct CMAcceleration { var x: Double // in g (9.8 m/s/s) var y: Double // in g var z: Double // in g } This raw data includes acceleration due to gravity So, if the device were laid flat, z would be 1.0 and x and y would be 0.0
  • 6. CS193p Fall 2017-18 Core Motion Checking the data (from existing periodic mechanism) var gyroData: CMGyroData? CMGyroData object provides var rotationRate: CMRotationRate struct CMRotationRate { var x: Double // in radians/s var y: Double // in radians/s var z: Double // in radians/s } Sign of the rotation data follows right hand rule The data above will be biased
  • 7. CS193p Fall 2017-18 Core Motion Checking the data (from existing periodic mechanism) var magnetometerData: CMMagnetometerData? CMMagnetometerData object provides var magneticField: CMMagneticField struct CMMagneticField { var x: Double // in microteslas var y: Double // in microteslas var z: Double // in microteslas } The data above will be biased
  • 8. CS193p Fall 2017-18 CMDeviceMotion CMDeviceMotion is a “combined” motion data source It uses information from all the hardware to improve the data from each. var deviceMotion: CMDeviceMotion? Acceleration Data in CMDeviceMotion var gravity: CMAcceleration var userAcceleration: CMAcceleration // gravity factored out using gyro Other information in CMDeviceMotion var rotationRate: CMRotationRate // bias removed from raw data using accelerometer var attitude: CMAttitude // device’s attitude (orientation) in 3D space class CMAttitude: NSObject // roll, pitch and yaw are in radians var roll: Double // around longitudinal axis passing through top/bottom var pitch: Double // around lateral axis passing through sides var yaw: Double // around axis with origin at CofG and ⊥ to screen directed down } var heading: Double // in degrees, where 0 is north (true or magnetic depending on frame)
  • 9. CS193p Fall 2017-18 CMDeviceMotion Reference Frame Magnetometer use in CMDeviceMotion can be controlled by setting its reference frame. Specify this when calling startDeviceMotionUpdates. xArbitraryZVertical // the default, does not use magnetometer xArbitraryCorrectedZVertical // uses magnetometer (if available) to correct yaw over time xMagnetic/TrueNorthZVertical // uses magnetometer for device position/heading in world These last two may require the user to calibrate the magnetometer. And for TrueNorth, location information (e.g. GPS/Wifi/Cellular) will also be required. North frames are necessary for apps that use things like Augmented Reality. To get heading, for example, you must use a MagneticNorth or TrueNorth reference frame. Always check to make sure the reference frame you want is available on the device … static func availableAttitudeReferenceFrames() -> CMAttitudeReferenceFrame
  • 10. CS193p Fall 2017-18 Core Motion Registering a block to receive Accelerometer data func startAccelerometerUpdatesToQueue(queue: OperationQueue, withHandler: CMAccelerometerHandler) typealias CMAccelerationHandler = (CMAccelerometerData?, Error?) -> Void queue can be an OperationQueue() you create or Operation.main (or current) Registering a block to receive Gyro data func startGyroUpdatesToQueue(queue: OperationQueue, withHandler: CMGyroHandler) typealias CMGyroHandler = (CMGyroData?, Error?) -> Void Registering a block to receive Magnetometer data func startMagnetometerUpdatesToQueue(queue: OperationQueue, withHandler: CMMagnetometerHandler) typealias CMMagnetometerHandler = (CMMagnetometerData?, Error?) -> Void
  • 11. CS193p Fall 2017-18 Core Motion Registering a block to receive DeviceMotion data func startDeviceMotionUpdates(using: CMAttitudeReferenceFrame, queue: OperationQueue, withHandler: (CMDeviceMotion?, Error?) -> Void) queue can be an OperationQueue() you create or Operation.mainQueue (or currentQueue) Errors … CMErrorDeviceRequiresMovement CMErrorTrueNorthNotAvailable CMErrorMotionActivityNotAvailable CMErrorMotionActivityNotAuthorized
  • 12. CS193p Fall 2017-18 Core Motion Setting the rate at which your block gets executed var accelerometerUpdateInterval: TimeInterval var gyroUpdateInterval: TimeInterval var magnetometerUpdateInterval: TimeInterval var deviceMotionUpdateInterval: TimeInterval It is okay to add multiple handler blocks Even though you are only allowed one CMMotionManager However, each of the blocks will receive the data at the same rate (as set above) (Multiple objects are allowed to poll at the same time as well, of course.)
  • 13. CS193p Fall 2017-18 Accelerometer Over Time Historical Accelerometer Data Sometimes you don’t need to look at the accelerometer in real time. You just want to know what happened over a period of time in the past. For example, if you want an idea of the user’s physical movement pattern. The class CMSensorRecorder can record (at 50hz) and then play back accelerometer data. Not all devices are capable of this (iPhone 7 and later, Apple Watch). isAccelerometerRecordingAvailable() -> Bool // whether this device can record Start recording data … func recordAccelerometer(forDuration: TimeInterval) // keep this short for performance Retrieving the recorded data … func accelerometerData(from: Date, to: Date) -> CMSensorDataList // 3 day max You enumerate over the CMAccelerometerData objects in a CMSensorDataList with for in … for dataPoint: CMRecordedAccelerometerData in sensorDataList { . . . }
  • 14. CS193p Fall 2017-18 Activity Monitoring Rough estimate of what the user is doing For example, stationary, walking, running, automotive, or cycling. You track this with a CMMotionActivityManager (not a CMMotionManager!). func startActivityUpdates(to: OperationQueue, withHandler: (CMMotionActivity?) -> Void) CMMotionActivity is one of the above activities. You can also query historical activity with … func queryActivityStarting(from: Date, to: Date, to: OperationQueue, withHandler: …)
  • 15. CS193p Fall 2017-18 Pedometer Pedometer Getting the user’s “step” information only makes sense over time. Create a CMPedometer and then send it the message … func startUpdates(from: Date, withHandler: (CMPedometerData?, Error?) -> Void) The from Date is allowed to be in the past (but only last 7 days are recorded). Your handler will be called periodically with the struct CMPedometerData which has … startDate and endDate of the data numberOfSteps, distance, averageActivePace, and currentPace during the time also floorsAscended and floorsDescended Altimeter Get relative altitude changes. func startRelativeAltitudeUpdates(to: OperationQueue, withHandler: (CMAltitudeData?, Error?)) CMAltitudeData has both change in altitude in meters and raw atmospheric pressure data.
  • 16. CS193p Fall 2017-18 Core Motion Checking the authorization status of hardware sensors Some information is considered “private” to the user (e.g. fitness data). Specifically CMPedometer, CMSensorRecorder, CMMotionActivityManager and CMAltimeter. iOS will automatically ask the user (once) for permission to access this information. You can find out what the status is at any time with this static func on each of these. static func authorizationStatus() -> CMAuthorizationStatus struct CMAuthorizationStatus { case notDetermined // user has not yet been asked case restricted // fitness data access disabled in Settings case denied // user has explicitly denied your app access case authorized // ready to go! } Lack of authorization may also show up as an error when you request data.
  • 17. CS193p Fall 2017-18 Demo Playing Card We’ll make our playing cards be affected by “real gravity” using the accelerometer.
  • 18. CS193p Fall 2017-18 UIImagePickerController Modal view controller to get media from camera or photo library i.e., you put it up with present(_:animated:completion:) Usage 1. Create it & set its delegate (it can’t do anything without its delegate) 2. Configure it (source, kind of media, user edibility, etc.) 3. Present it 4. Respond to delegate methods when user is done/cancels picking the media What the user can do depends on the platform Almost all devices have cameras, but some can record video, some can not You can only offer camera or photo library on iPad (not both together at the same time) As with all device-dependent API, we want to start by check what’s available … static func isSourceTypeAvailable(sourceType: UIImagePickerControllerSourceType) -> Bool Source type is .photoLibrary or .camera or .savedPhotosAlbum (camera roll)
  • 19. CS193p Fall 2017-18 UIImagePickerController But don’t forget that not every source type can give video So, you then want to check ... static func availableMediaTypes(for: UIImagePickerControllerSourceType) -> [String]? Depending on device, will return one or more of these ... kUTTypeImage // pretty much all sources provide this, hardly worth checking for even kUTTypeLivePhoto // must also say kUTTypeImage for this one to work kUTTypeMovie // audio and video together, only some sources provide this You can get even more specific about cameras (Though usually this is not necessary) static func isCameraDeviceAvailable(UIImagePickerControllerCameraDevice) -> Bool returns .rear or .front There are other camera-specific interrogations too, for example … static func isFlashAvailableForCameraDevice(UIImagePickerControllerCameraDevice) -> Bool These are declared in the MobileCoreServices framework. import MobileCoreServices
  • 20. CS193p Fall 2017-18 UIImagePickerController Set the source and media type you want in the picker Example setup of a picker for capturing video (kUTTypeMovie) … (From here out, UIImagePickerController will be abbreviated UIIPC for space reasons.) let picker = UIImagePickerController() let mediaTypeMovie = kUTTypeMovie as String picker.delegate = self // self must implement UINavigationControllerDelegate too if UIIPC.isSourceTypeAvailable(.camera) { picker.sourceType = .camera if let availableTypes = UIIPC.availableMediaTypesForSourceType(.camera) { if availableTypes.contains(mediaTypeMovie) { picker.mediaTypes = [mediaTypeMovie] // proceed to put the picker up } } }
  • 21. CS193p Fall 2017-18 UIImagePickerController Editability var allowsEditing: Bool If true, then the user will have opportunity to edit the image/video inside the picker. When your delegate is notified that the user is done, you’ll get both raw and edited versions. Limiting Video Capture var videoQuality: UIImagePickerControllerQualityType .typeMedium // default .typeHigh .type640x480 .typeLow .typeIFrame1280x720 // native on some devices .typeIFrame960x540 // native on some devices var videoMaximumDuration: TimeInterval // defaults to 10 minutes
  • 22. CS193p Fall 2017-18 UIImagePickerController Present the picker present(picker, animated: true, completion: nil) On iPad, if you are not offering Camera (just photo library), you must present with popover. If you are offering the Camera on iPad, then full-screen is preferred. Remember: on iPad, it’s Camera OR Photo Library (not both at the same time). Delegate will be notified when user is done func imagePickerController(UIIPC, didFinishPickingMediaWithInfo info: [String:Any]) { // extract image/movie data/metadata here from info, more on the next slide picker.presentingViewController?.dismiss(animated: true) { } } Also dismiss it when cancel happens func imagePickerControllerDidCancel(UIIPC) { picker.presentingViewController?.dismiss(animated: true) { } }
  • 23. CS193p Fall 2017-18 UIImagePickerController What is in that info dictionary? UIImagePickerControllerMediaType // kUTTypeImage, kUTTypeMovie UIImagePickerControllerOriginalImage // UIImage UIImagePickerControllerEditedImage // UIImage UIImagePickerControllerImageURL // URL (in a temp location, so move it to keep it) UIImagePickerControllerCropRect // CGRect (in an NSValue) UIImagePickerControllerMediaMetadata // Dictionary of info about the image UIImagePickerControllerLivePhoto // a PHLivePhoto UIImagePickerControllerPHAsset // a PHAsset (see PHPhotoLibrary) UIImagePickerControllerMediaURL // URL of the video if kUTTypeMovie
  • 24. CS193p Fall 2017-18 UIImagePickerController Saving taken images or video into the device’s photo library You can save to the user’s Camera Roll … func UIImageWriteToSavedPhotosAlbum( _ image: UIImage, _ target: Any?, // the object to send selector to when finished writing _ selector: Selector? // selector to send to target when finished writing _ context: UnsafeMutableRawPointer? // passed to the selector ) It’s a bummer that this isn’t closure-based, but it is what it is. This is a very simple and convenient way to do this. But this only makes sense if the user only occasionally would want to save an image. Otherwise, you’ll want to integrate with the Photos application: checkout PHPhotoLibrary. Of course, you could also save the image into your own sandbox. You’d do that if the captured images only make sense inside your own app.
  • 25. CS193p Fall 2017-18 UIImagePickerController In general, much more sophisticated media capture is available This UIImagePickerController API is pretty simple, but more powerful API exists. Check out both PHPhotoLibrary and AVCaptureDevice.
  • 26. CS193p Fall 2017-18 UIImagePickerController Overlay View var cameraOverlayView: UIView Be sure to set this view’s frame properly. Camera is always full screen, so use UIScreen.main’s bounds property. If you use the built-in controls at the bottom, you might want your view to be smaller. Hiding the normal camera controls (at the bottom) var showsCameraControls: Bool Will leave a blank area at the bottom of the screen (camera’s aspect 4:3, not same as screen’s). With no controls, you’ll need an overlay view with a “take picture” (at least) button. That button should send takePicture() or (startVideoCapture()) to the picker. Don’t forget to dismiss when you are done taking pictures. You can zoom or translate the image while capturing var cameraViewTransform: CGAffineTransform For example, you might want to scale the image up to full screen (some of it will get clipped).
  • 27. CS193p Fall 2017-18 Core Image and Vision Processing Images Core Image is a powerful and efficient framework for applying filters to your images. Has a couple of hundred filters to choose from (blur, depth, comparison, colors, smoothing, etc.). Vision framework provides powerful feature detection in images (e.g. faces, barcodes, etc.). Core Image also has some feature detection API. Check out Core Image and Vision in the documentation.
  • 28. CS193p Fall 2017-18 Demo Code Download the Emoji Art demo from today’s lecture. Download the Playing Card demo from today’s lecture.