cs193p - lecture 15cs193p - lecture 15 iphone application development iphone device apis location,...

115
CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management 1 Friday, February 26, 2010

Upload: others

Post on 19-Apr-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

CS193P - Lecture 15iPhone Application Development

iPhone Device APIsLocation, Accelerometer & Camera

Battery Life & Power Management

1Friday, February 26, 2010

xkozima
Typewritten Text
デバイス API 位置・加速度・カメラ
xkozima
Typewritten Text
Page 2: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Announcements• Paparazzi 4 due Friday night at 11:59PM

■ Late days: use ’em if you’ve got ’em

• Work on final projects!

2Friday, February 26, 2010

Page 3: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Today’s Topics• Hardware features

■ Image Picker & Camera■ Location■ Accelerometer

• Battery Life & Power Management

3Friday, February 26, 2010

xkozima
Typewritten Text
イメージピッカーとカメラ
xkozima
Typewritten Text
位置(センサ)
xkozima
Typewritten Text
Page 4: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Lots of Cool Features

4Friday, February 26, 2010

Page 5: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Device HardwareCamera

5Friday, February 26, 2010

Page 6: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Device HardwareCore location

6Friday, February 26, 2010

Page 7: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Device HardwareAccelerometers

7Friday, February 26, 2010

Page 8: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Limited Simulator Support

8Friday, February 26, 2010

Page 9: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Image Picker

9Friday, February 26, 2010

Page 10: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

The Image Picker InterfaceThe camera

10Friday, February 26, 2010

xkozima
Typewritten Text
カメラから画像選択
xkozima
Typewritten Text
Page 11: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

The Image Picker InterfaceSaved photos

11Friday, February 26, 2010

xkozima
Typewritten Text
保存された写真から画像選択
xkozima
Typewritten Text
Page 12: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

The Image Picker InterfaceThe photo library

12Friday, February 26, 2010

xkozima
Typewritten Text
写真アルバムから画像選択
xkozima
Typewritten Text
Page 13: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

The Image Picker InterfaceDisplaying the interface

• UIImagePickerController class■ Use as-is (no subclassing)■ Handles all user and device interactions■ UIViewController Subclass

• UIImagePickerControllerDelegate protocol■ Implemented by your delegate object

13Friday, February 26, 2010

xkozima
Typewritten Text
サブクラス化せずにそのまま使う
xkozima
Typewritten Text
デリゲートを指定して 自分のコントローラで メソッドを処理
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 14: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Displaying the Image PickerSteps for using

• Check the source availability• Assign a delegate object• Present the controller modally

14Friday, February 26, 2010

xkozima
Typewritten Text
イメージピッカー の表示
xkozima
Typewritten Text
xkozima
Typewritten Text
画像取得元が利用可能かをチェック
xkozima
Typewritten Text
デリゲートを指定
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
モーダルビューとして ピッカーコントローラを表示
xkozima
Typewritten Text
Page 15: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Displaying the Image PickerCalled from a view controller object

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){ UIImagePickerController* picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypeCamera; picker.delegate = self;

[self presentModalViewController:picker animated:YES];}

15Friday, February 26, 2010

xkozima
Typewritten Text
カメラの存在を    チェック
xkozima
Typewritten Text
  
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
ピッカーを生成      設定
xkozima
Typewritten Text
モーダルビュー としてピッカーを 表示
xkozima
Typewritten Text
Page 16: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Selecting an Image

20Friday, February 26, 2010

Page 17: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Defining Your Delegate ObjectThe UIImagePickerControllerDelegate protocol

• Two methods:

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(NSDictionary*)editingInfo;

- (void)imagePickerControllerDidCancel: (UIImagePickerController*)picker;

21Friday, February 26, 2010

xkozima
Typewritten Text
画像が選択されたときに呼び出されるメソッド
xkozima
Typewritten Text
xkozima
Typewritten Text
キャンセル時に呼び出されるメソッド
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 18: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Defining Your Delegate ObjectThe accept case

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(NSDictionary*)editingInfo { // Save or use the image here.

// Dismiss the image picker. [self dismissModalViewControllerAnimated:YES]; [picker release];}

22Friday, February 26, 2010

xkozima
Typewritten Text
画像が選択されたとき
xkozima
Typewritten Text
xkozima
Typewritten Text
画像を煮るなり焼くなり...
xkozima
Typewritten Text
モーダルビューを閉じる
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 19: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Defining Your Delegate ObjectThe cancel case

- (void)imagePickerControllerDidCancel: (UIImagePickerController*)picker{ // Dismiss the image picker. [self dismissModalViewControllerAnimated:YES]; [picker release];}

26Friday, February 26, 2010

xkozima
Typewritten Text
キャンセル時
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 20: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Manipulating the Returned Image Allowing users to edit returned images

• If allowsImageEditing property is YES:■ User allowed to crop the returned image■ Image metadata returned in editingInfo

29Friday, February 26, 2010

xkozima
Typewritten Text
編集可能フラグが YES なら   ユーザは部分選択可能
Page 21: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Manipulating the Returned Image Allowing users to edit returned images

30Friday, February 26, 2010

Page 22: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

The editingInfo dictionaryManipulating the Returned Image

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(NSDictionary*)editingInfo { // Save or use the image here.

// Dismiss the image picker. [self dismissModalViewControllerAnimated:YES]; [picker release];}

31Friday, February 26, 2010

Page 23: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Manipulating the Returned Image The editingInfo dictionary

• Original image in UIImagePickerControllerOriginalImage key• Crop rectangle in UIImagePickerControllerCropRect key

32Friday, February 26, 2010

xkozima
Typewritten Text
これらのキーを与えれば,   オリジナル画像,編集後の Rect が得られる
xkozima
Typewritten Text
Page 24: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Augmented RealityWalk around looking through a camera.What could possibly go wrong?

@property BOOL showsCameraControls;@property(retain) UIView cameraOverlayView;@property CGAffineTransform cameraViewTransform;

33Friday, February 26, 2010

Page 25: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

The Image Picker InterfaceCustom Camera Interface

34Friday, February 26, 2010

Page 26: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Managing Image DataAvoid retaining images

Application

35Friday, February 26, 2010

Page 27: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Saving ImagesWriting to the saved photos album

• UIImageWriteToSavedPhotosAlbum function■ Photos can be downloaded to iPhoto by user■ Optional completion callback

36Friday, February 26, 2010

xkozima
Typewritten Text
写真アルバムへの保存
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 28: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Saving VideosWriting to the saved photos album

• UIVideoAtPathIsCompatibleWithSavedPhotosAlbum

• UISaveVideoAtPathToSavedPhotosAlbum function■ Videos can be downloaded to iPhoto by user■ Optional completion callback

37Friday, February 26, 2010

xkozima
Typewritten Text
写真アルバムへの保存
xkozima
Typewritten Text
Page 29: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Available in the Simulator

38Friday, February 26, 2010

Page 30: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Key TipsUsing UIImagePickerController effectively

• Always check the source availability• Your delegate methods do the cleanup• Be frugal with images• Available in the simulator

39Friday, February 26, 2010

xkozima
Typewritten Text
カメラ存在のチェックを        忘れずに
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 31: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Core Location

40Friday, February 26, 2010

Page 32: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Core LocationWhat is it?

41Friday, February 26, 2010

Page 33: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Core LocationWhat is it?

Location ring

Activate service

41Friday, February 26, 2010

Page 34: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Core LocationHow?

42Friday, February 26, 2010

xkozima
Typewritten Text
携帯基地局
xkozima
Typewritten Text
Page 35: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

How?Core Location

43Friday, February 26, 2010

xkozima
Typewritten Text
Wifi の アクセスポイント
xkozima
Typewritten Text
Page 36: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Core LocationHow?

44Friday, February 26, 2010

xkozima
Typewritten Text
GPS 衛星
xkozima
Typewritten Text
Page 37: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Core LocationLocation Technologies

Bootstrap

46Friday, February 26, 2010

xkozima
Typewritten Text
Page 38: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Core LocationLocation Technologies

Cross-check

47Friday, February 26, 2010

Page 39: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Core LocationLocation Technologies

Complement

48Friday, February 26, 2010

Page 40: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Core Location Framework

49Friday, February 26, 2010

Page 41: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Core Location FrameworkThe core classes and protocols

• Classes■ CLLocationManager■ CLLocation

• Protocol■ CLLocationManagerDelegate

50Friday, February 26, 2010

xkozima
Typewritten Text
デリゲートのプロトコル
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 42: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Core Location FrameworkCLLocationManagerDelegate protocol

• Two optional methods

• Called asynchronously on main thread• Issues movement-based updates

- (void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation;

- (void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error;

51Friday, February 26, 2010

xkozima
Typewritten Text
位置に変化が あったとき
xkozima
Typewritten Text
失敗したとき
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 43: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Getting a LocationStarting the location service

CLLocationManager* locManager = [[CLLocationManager alloc] init];

locManager.delegate = self;[locManager startUpdatingLocation];

54Friday, February 26, 2010

xkozima
Typewritten Text
位置管理者の 生成   通知先を設定 スタート
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 44: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

- (void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation{

NSTimeInterval howRecent = [newLocation.timestamp timeIntervalSinceNow];

if (howRecent < -10) return;

if (newLocation.horizontalAccuracy > 100) return;

// Use the coordinate data. double lat = newLocation.coordinate.latitude; double lon = newLocation.coordinate.longitude;}

Getting a LocationUsing the event data

59Friday, February 26, 2010

xkozima
Typewritten Text
位置に 変化が あった とき
xkozima
Typewritten Text
xkozima
Typewritten Text
誤差 100m超
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 45: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{ // Use the coordinate data. CLLocationDirection heading = newHeading.trueHeading;}

Getting a HeadingUsing the event data

60Friday, February 26, 2010

xkozima
Typewritten Text
コンパス 方角に 変化
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 46: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Power Play (beat Canada again): CLLocationManager Properties

61Friday, February 26, 2010

Page 47: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Desired AccuracyChoosing an appropriate accuracy level

• Choose an appropriate accuracy level■ Higher accuracy impacts power consumption■ Lower accuracy is “good enough” in most cases

• Can change accuracy setting later if needed• Actual accuracy reported in CLLocation object

CLLocationManager* locManager = [[CLLocationManager alloc] init];

locManager.desiredAccuracy = kCLLocationAccuracyBest;

62Friday, February 26, 2010

Page 48: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Distance FilterChoosing an appropriate update threshold

• New events delivered when threshold exceeded

CLLocationManager* locManager = [[CLLocationManager alloc] init];

locManager.distanceFilter = 3000;

63Friday, February 26, 2010

Page 49: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

• Restart the service later as needed

Stopping the Service

CLLocationManager* locManager = [[CLLocationManager alloc] init];[locManager startUpdatingLocation];

...

[locManager stopUpdatingLocation];

64Friday, February 26, 2010

Page 50: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Responding to ErrorsUser may deny use of the location service

• Results in a kCLErrorDenied error

• Protects user privacy• Occurs on a per-application basis

65Friday, February 26, 2010

Page 51: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Responding to ErrorsLocation may be unavailable

• Results in a kCLErrorLocationUnknown error

• Likely just temporary• Scan continues in background

66Friday, February 26, 2010

Page 52: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Limited Simulator Support

67Friday, February 26, 2010

Page 53: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Accelerometers

68Friday, February 26, 2010

xkozima
Typewritten Text
加速度センサ
xkozima
Typewritten Text
Page 54: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

What Are Accelerometers?Measure changes in force

70Friday, February 26, 2010

xkozima
Typewritten Text
(おもりにかかる)力を計測
xkozima
Typewritten Text
Page 55: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

What Are Accelerometers?Measure changes in force

71Friday, February 26, 2010

Page 56: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

What Are Accelerometers?Measure changes in force

71Friday, February 26, 2010

Page 57: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

AccelerometersWhat are the uses?

72Friday, February 26, 2010

Page 58: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

AccelerometersWhat are the uses?

73Friday, February 26, 2010

Page 59: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Kinds of OrientationThe physical vs the interface

• Physical Orientation■ How is the device positioned?

• Interface Orientation■ Where is the status bar?

• Examples: Photos & Safari

74Friday, February 26, 2010

Page 60: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

75Friday, February 26, 2010

Page 61: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

76Friday, February 26, 2010

Page 62: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

77Friday, February 26, 2010

Page 63: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

78Friday, February 26, 2010

Page 64: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

79Friday, February 26, 2010

Page 65: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

79Friday, February 26, 2010

Page 66: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

80Friday, February 26, 2010

Page 67: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

81Friday, February 26, 2010

Page 68: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

82Friday, February 26, 2010

Page 69: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

82Friday, February 26, 2010

Page 70: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

83Friday, February 26, 2010

Page 71: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Orientation-Related ChangesGetting the physical orientation

• UIDevice class■ Start notifications

■ beginGeneratingDeviceOrientationNotifications■ Get Orientation

■ UIDeviceOrientationDidChangeNotification delivered to registered observers

■ orientation property■ Stop notifications

■ endGeneratingDeviceOrientationNotifications

84Friday, February 26, 2010

Page 72: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Orientation-Related ChangesGetting the interface orientation

• UIApplication class■ statusBarOrientation property■ Defines interface orientation, not device orientation

• UIViewController class■ interfaceOrientation property

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation

85Friday, February 26, 2010

Page 73: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

ShakeUndo!

• UIEvent type■ @property(readonly) UIEventType type;■ @property(readonly) UIEventSubtype subtype;

■ UIEventTypeMotion■ UIEventSubtypeMotionShake

86Friday, February 26, 2010

xkozima
Typewritten Text
振る(シェイク!=Undo!)
xkozima
Typewritten Text
Page 74: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Orientation changes are nice, but…

87Friday, February 26, 2010

xkozima
Typewritten Text
向きが分かるのもいいけれど...
Page 75: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Wii Want Raw Data

0.5g

0.75g

1.0g

88Friday, February 26, 2010

xkozima
Typewritten Text
生データを欲しいよね
xkozima
Typewritten Text
Page 76: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Wii Want Raw Data

0.5g

0.75g

1.0g

88Friday, February 26, 2010

Page 77: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

The Accelerometer InterfaceGetting the raw accelerometer data

• Part of the UIKit framework• Delivers 3-axis data• Configurable update frequency (approx 10–100Hz)• Delegate-based event delivery

89Friday, February 26, 2010

xkozima
Typewritten Text
3軸それぞれの生データ
xkozima
Typewritten Text
デリゲートで通知される
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 78: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Device Axis Orientation

+X

-X

+Y

-Y

-Z

+Z

90Friday, February 26, 2010

Page 79: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

The Accelerometer InterfaceGetting the raw accelerometer data

• Classes■ UIAccelerometer■ UIAcceleration

• Protocol■ UIAccelerometerDelegate

91Friday, February 26, 2010

xkozima
Typewritten Text
デリゲートのプロトコル
xkozima
Typewritten Text
Page 80: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Configuring the AccelerometerStarting the event delivery

- (void)enableAccelerometerEvents{ UIAccelerometer* theAccel = [UIAccelerometer sharedAccelerometer]; theAccel.updateInterval = 1/50; // 50 Hz theAccel.delegate = self;}

92Friday, February 26, 2010

xkozima
Typewritten Text
まずは仕込み (イベント通知を設定)
xkozima
Typewritten Text
Page 81: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Event delivery begins as soon asyou assign the delegate

96Friday, February 26, 2010

Page 82: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Defining Your Delegate ObjectProcessing the accelerometer data

• Only one delegate per application• Delivered asynchronously to main thread

- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration{ // Get the event data UIAccelerationValue x, y, z; x = acceleration.x; y = acceleration.y; z = acceleration.z;

// Process the data... }

99Friday, February 26, 2010

xkozima
Typewritten Text
設定しておいた 時間間隔で このメソッドが 呼び出される
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 83: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Configuring the AccelerometerChoosing an appropriate update frequency

• System range is approximately 10–100Hz• Frequency should be based on need

■ Determine the minimum frequency for your needs■ Don’t update too frequently

• Target ranges■ Game input: 30–60 Hz■ Orientation detection: 10–20 Hz

100Friday, February 26, 2010

xkozima
Typewritten Text
必要に応じた時間間隔で
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 84: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Disabling Event DeliveryStopping the event delivery

- (void)disableAccelerometerEvents{ UIAccelerometer* theAccel = [UIAccelerometer sharedAccelerometer];

theAccel.delegate = nil;}

101Friday, February 26, 2010

xkozima
Typewritten Text
イベント通知をストップさせるには
xkozima
Typewritten Text
Page 85: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Filtering Accelerometer DataUse filters to isolate data components

• Low-pass filter■ Isolates constant acceleration■ Used to find the device orientation

• High-pass filter■ Shows instantaneous movement only■ Used to identify user-initiated movement

102Friday, February 26, 2010

xkozima
Typewritten Text
向きを知るには ローパスフィルタ
xkozima
Typewritten Text
xkozima
Typewritten Text
ジェスチャを知るには ハイパスフィルタ
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 86: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Filtering Accelerometer DataExamining the accelerometer data

-1.0g

f(t)

103Friday, February 26, 2010

Page 87: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Filtering Accelerometer DataBut, to apply a filter…

f(t) => F(ω)Fourier Transform

104Friday, February 26, 2010

xkozima
Typewritten Text
フーリエ変換すると
xkozima
Typewritten Text
Page 88: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Filtering Accelerometer DataChanging to the frequency domain

f(t) F(ω)

Frequency (ω)

105Friday, February 26, 2010

Page 89: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Filtering Accelerometer DataBut if we shake the device…

106Friday, February 26, 2010

Page 90: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

We see something more interesting…Filtering Accelerometer Data

f(t)

Frequency (ω)

F(ω)

107Friday, February 26, 2010

Page 91: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

We see something more interesting…Filtering Accelerometer Data

f(t)

Frequency (ω)

F(ω)

107Friday, February 26, 2010

Page 92: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Frequency (ω)

F(ω)

Applying a low-pass filterFiltering Accelerometer Data

f(t)108Friday, February 26, 2010

Page 93: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Frequency (ω)

F(ω)

Applying a low-pass filterFiltering Accelerometer Data

f(t)108Friday, February 26, 2010

Page 94: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Frequency (ω)

F(ω)

Applying a low-pass filterFiltering Accelerometer Data

f(t)108Friday, February 26, 2010

Page 95: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Filtering Accelerometer DataApplying a low-pass filter

• Simple low-pass filter example

#define FILTERFACTOR 0.1

value = (newAcceleration * FILTERFACTOR) + (previousValue * (1.0 - FILTERFACTOR));

previousValue = value;

109Friday, February 26, 2010

xkozima
Typewritten Text
注)ちょっとシンプルすぎるローパスフィルタ (興味あるひとは FIR をググる)
Page 96: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Frequency (ω)

F(ω)

Applying a high-pass filterFiltering Accelerometer Data

f(t)110Friday, February 26, 2010

Page 97: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Frequency (ω)

F(ω)

Applying a high-pass filterFiltering Accelerometer Data

f(t)110Friday, February 26, 2010

Page 98: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Filtering Accelerometer DataApplying a high-pass filter

• Simple high-pass filter example

#define FILTERFACTOR 0.1

lowPassValue = (newAcceleration * FILTERFACTOR) + (previousValue * (1.0 - FILTERFACTOR));

previousLowPassValue = lowPassValue;

highPassValue = newAcceleration - lowPassValue;

111Friday, February 26, 2010

xkozima
Typewritten Text
低周波成分を引けば 高周波成分が残る
xkozima
Typewritten Text
Page 99: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Filtering Accelerometer DataBubble Level sample (low-pass filter)

112Friday, February 26, 2010

xkozima
Typewritten Text
水平器
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 100: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Demo

113Friday, February 26, 2010

Page 101: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Filtering Accelerometer DataBubble Level sample (low-pass filter)

- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration{ accelerationX = acceleration.x * kFilteringFactor + accelerationX * (1.0 - kFilteringFactor); accelerationY = acceleration.y * kFilteringFactor + accelerationY * (1.0 - kFilteringFactor);

currentRawReading = atan2(accelerationY,accelerationX); float calibtratedAngle = [self calibratedAngleFromAngle: currentRawReading];

[levelView updateToInclinationInRadians:calibratedAngle]; }

114Friday, February 26, 2010

Page 102: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Demo

115Friday, February 26, 2010

Page 103: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

No Simulator Support

116Friday, February 26, 2010

Page 104: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Key TipsUsing the Accelerometers Effectively

• Use UIViewControllers• Use filters to isolate raw data components• Disable accelerometer updates when not needed

■ Set your accelerometer delegate to nil

117Friday, February 26, 2010

xkozima
Typewritten Text
Page 105: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Summary• Take advantage of the device APIs, but…• For image picker, always check source availability• For hardware-based features, turn them off when not needed

118Friday, February 26, 2010

Page 106: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Battery Life & Power Management

119Friday, February 26, 2010

xkozima
Typewritten Text
これについては割愛します
Page 107: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Power ManagementSmall devices need advanced power management

• Total power consumption■ Laptops: ~20-60W■ iPhone: 500 mW to 2.5W

• Dynamic clocking• Clock gating and power gating

■ Turning blocks on and off continuously

120Friday, February 26, 2010

Page 108: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Power ConsumptionEverything consumes power

• Radios – up to ~2W■ Baseband, Wi-Fi, Bluetooth, GPS

• CPU/GPU – up to ~800 mW• Display – up to ~200 mW• Hardware modules – ~10s of mWs• Keeping the system awake – enormous impact

121Friday, February 26, 2010

Page 109: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Be aware of power consumptionBattery Life

122Friday, February 26, 2010

Page 110: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Be aware of power consumptionBattery Life

122Friday, February 26, 2010

Page 111: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Power Consumption - RadiosThe network

• Transmitting is the most expensive operation• Minimize the amount of transmitted data• Avoid chatty protocols• Transmit/receive in bursts• Use compact data formats• Core Location

■ Stop the location service once you have a location fix■ Request only the location accuracy that you need

123Friday, February 26, 2010

Page 112: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Power Consumption - CPU/GPUAll about performance

• Reduce CPU usage• Use Sample or Shark• Stress the GPU less – fewer layers, smaller textures, etc.

124Friday, February 26, 2010

Page 113: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Power Consumption - Hardware ModulesAccelerometer, NAND, others

• Turn off what you don’t need• Accelerometer

■ Set the UIAccelerometer delegate to nil■ Support orientation changes only as needed

• NAND■ Access the disk less – use the System Usage instrument

125Friday, February 26, 2010

Page 114: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Power Consumption - StandbyLet the system sleep

• Battery life drops from 250+ hours to <12 hours without sleep• Don’t disable the idle timer• Don’t play audio except when you need to

126Friday, February 26, 2010

Page 115: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010

Questions?

127Friday, February 26, 2010