SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Downloaden Sie, um offline zu lesen
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.

Weitere ähnliche Inhalte

Ähnlich wie Camera And Email

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-batterylifethatisjicheng687
 
Design and implementation of GPS Tracker
Design and implementation of GPS TrackerDesign and implementation of GPS Tracker
Design and implementation of GPS TrackerVignesh 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 ProcessingMatthias Trapp
 
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 AnalysisFastly
 
Rapid Laser Scanning the process
Rapid Laser Scanning the processRapid Laser Scanning the process
Rapid Laser Scanning the processSeeview 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 CS5Chris Griffith
 
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 SwitzerlandFrançois Garillot
 
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 KafsiSpark Summit
 
Monitoring as Software Validation
Monitoring as Software ValidationMonitoring as Software Validation
Monitoring as Software ValidationBioDec
 
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 2018Amazon 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 editiontransight
 
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 MachineAndrew 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
 

Ähnlich wie 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
 
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
 
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
 
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...
 

Mehr von SV.CO

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

Mehr von 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
 

Kürzlich hochgeladen

MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 

Kürzlich hochgeladen (20)

MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 

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.