1、关于iOS设备上的摄像头
实现在摄像头上获取图像的方法:使用UIImagePickerController。这个视图控制器不使用storyboard生成,因此需要使用alloc、init方法手动建立。显示这个ViewController的方法需要调用presentViewController::animated:completion。执行步骤:①建立视图控制器实例,并设置代理;②配置(数据源、媒体类型、是否可编辑);③显示视图控制器;④完成获取图像后有代理的方法响应。
具体实现例子:
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; //表示希望通过摄像头拍摄图像
if(![UIImagePickerController isSourceTypeAvailable:sourceType])//检测当前设备是否支持获取指定媒体类型的数据
{
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];//新建UIIPC实例
imagePickerController.delegate = self; // 设置委托为当前类
imagePickerController.sourceType = sourceType;//指定媒体类型
imagePickerController.allowsEditing = YES;//设置为可以编辑
[self presentViewController:imagePickerController animated:YES completion:nil]; //需要以模态的形式展示
[imagePickerController release];
拍摄(或读取)图像完成后,还需调用delegate方法执行后续操作:
#pragma mark - #pragma mark UIImagePickerController Method //完成拍照 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissViewControllerAnimated:YES completion:^{}]; UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; if (image == nil) image = [info objectForKey:UIImagePickerControllerOriginalImage]; [self performSelector:@selector(saveImage:) withObject:image]; } //用户取消拍照 -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:nil]; } //将照片保存到disk上 -(void)saveImage:(UIImage *)image { NSData *imageData = UIImagePNGRepresentation(image); if(imageData == nil) { imageData = UIImageJPEGRepresentation(image, 1.0); } NSDate *date = [NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyyMMddHHmmss"]; _fileName = [[[formatter stringFromDate:date] stringByAppendingPathExtension:@"png"] retain]; NSURL *saveURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:_fileName]; [imageData writeToURL:saveURL atomically:YES]; }
2、Core Motion简介
Core Motion代表了iOS设备中的陀螺仪等模块,其提供的API可以调用设备的动作传感器,检测当前设备的动作状态。实现该功能的类为CMMotionManager,该类同样适用alloc、init创建,但是一个app中只能有一个该类的实例。该对象可认为是一个全局资源。
适用方法:①检测硬件是否支持;②启动传感器获取数据;③检测是否正在获取数据;④停止传感器;
(1)加速度
//获取三维加速度 CMAccelerometerData *newestAccel = motionManager.accelerometerData; double accelerationX = newestAccel.acceleration.x; double accelerationY = newestAccel.acceleration.y; double accelerationZ = newestAccel.acceleration.z;(2)各个方向的重力以及设备倾斜角
double gravityX = motionManager.deviceMotion.gravity.x; double gravityY = motionManager.deviceMotion.gravity.y; double gravityZ = motionManager.deviceMotion.gravity.z; double zTheta = atan2(gravityZ,sqrtf(gravityX*gravityX+gravityY*gravityY))/M_PI*180.0;//与水平面的角度 double xyTheta = atan2(gravityX,gravityY)/M_PI*180.0;//设备自身旋转的角度
(3)陀螺仪数据
角速度:
CMRotationRate rotationRate = motionManager.deviceMotion.rotationRate; double rotationX = rotationRate.x; double rotationY = rotationRate.y; double rotationZ = rotationRate.z;空间位置的欧拉角
double roll = motionManager.deviceMotion.attitude.roll; double pitch = motionManager.deviceMotion.attitude.pitch; double yaw = motionManager.deviceMotion.attitude.yaw;空间位置的四元数
double w = motionManager.deviceMotion.attitude.quaternion.w; double wx = motionManager.deviceMotion.attitude.quaternion.x; double wy = motionManager.deviceMotion.attitude.quaternion.y; double wz = motionManager.deviceMotion.attitude.quaternion.z;
3、应用的状态
当应用UI开始/停止接收消息时,应用的代理会分别调用以下方法:
- (void)applicationDidBecomeActive:(UIApplication *)sender; - (void)applicationWillResignActive:(UIApplication *)sender;同时将会发送两条广播消息:UIApplicationDidBecomeActiveNotification和UIApplicationWillResignActive Notification。可以使用这两个消息用于暂停和重启应用。
当应用进入后台时/从后台重启时,会调用:
- (void)applicationDidEnterBackground:(UIApplication *)sender; - (void)applicationWillEnterForgound:(UIApplication *)sender;并发送 UIApplicationDidEnterBackgroundNotification和UIApplicationWillEnterForgroundNotification。
除此之外还有其他代理方法,如本地消息、UI恢复状态、数据保护、打开URL等。