IOS ipad拍照问题

来源:互联网 发布:java经典程序 源代码 编辑:程序博客网 时间:2024/05/22 10:44



刚接触cocos2d,短短的时间内就喜欢上了它,第一个项目要用到ipad的拍照功能,所以在这里分享一下自己学习的一点东西。

首先,在iphone和ipad中,拍照的实现是不同的,在iphone中,

if([UIImagePickerController   isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

{

picker = [[UIImagePickerController alloc] init];

picker.delegate = self;

picker.sourceType = UIImagePickerControllerSourceTypeCamera;

picker.allowsEditing = NO;

picker.showsCameraControllers = YES;

       [self presentModalViewController:m_imagePicker animated:YES];
       [m_imagePicker release];
   }else {
       UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@”" delegate:nil cancelButtonTitle:@”Close” otherButtonTitles:nil];
       [alert show];
       [alert release];
   }

即可调用相机拍照,但在ipad中,需要通过UIPopoverController来实现,或者通过openGL也可实现,如下

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

{

picker = [[UIImagePickerController alloc] init];

picker.delegate = self;

picker.sourceType = UIImagePickerControllerSourceTypeCamera;

picker.allowsEditing = NO;

picker.modalPresentationStyle = UIModalPresentationCurrentContext;

UIViewController * myViewController = [[UIViewController alloc] init];

[[[CCDirector sharedDirector] openGLView] addSubview:myViewController.view];

[myViewController presentModalViewController:picker animated:YES];

[myViewController setModalPresentationStyle:UIModalPresentationCurrentContext];

}

对于拍照的控制需要使用UIImagePickerControllerDelegate的三个方法

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

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;

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

一般来说,如果item中包含image和video时,需要使用didFinishPickingMediaWithInfo的方法,拍照后在未保存图片之前,照片的信息会存放在缓存中,这时通过保存图片信息的字典中内容可以获取所拍照片,实现如下

- (void) imagePickerController:(UIImagePickerController *)picker1 didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage * photo = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

}

所得的照片有一个只读的属性imageOrientation,它记录照片拍摄时的方向,在某些情况下,这个属性是非常有用的。还有一点容易忽略的东西,使用ipad拍的照片大小是960*720,而不是1024*768。

刚刚开始学习,只是一些很简单的东西,但仍然有所收获,期待着自己可以学到更多,喜欢cocos2d的朋友们一起努力吧^_^

0 0
原创粉丝点击