ios开发中怎样调用相机实现拍照功能

 我来答
就烦条0o
2016-07-10 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46492
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部
打开相机:

[cpp] view plain copy
//先设定sourceType为相机,然后判断相机是否可用(ipod)没相机,不可用将sourceType设定为相片库
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
// if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
// sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// }
//sourceType = UIImagePickerControllerSourceTypeCamera; //照相机
//sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //图片库
//sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; //保存的相片
UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化
picker.delegate = self;
picker.allowsEditing = YES;//设置可编辑
picker.sourceType = sourceType;
[self presentModalViewController:picker animated:YES];//进入照相界面
[picker release];

打开相册:(区分pad和iphone)
for iphone:

[cpp] view plain copy
UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//pickerImage.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];

}
pickerImage.delegate = self;
pickerImage.allowsEditing = NO;
[self presentModalViewController:pickerImage animated:YES];
[pickerImage release];

for ipad:
[cpp] view plain copy
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; //保存的相片
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;//是否允许编辑
picker.sourceType = sourceType;
/*
如果从一个导航按钮处呈现,使用:
presentPopoverFromBarButtonItem:permittedArrowDirections:animated:;
如果要从一个视图出呈现,使用:
presentPopoverFromRect:inView:permittedArrowDirections:animated:

如果设备旋转以后,位置定位错误需要在父视图控制器的下面方法里面重新定位:
didRotateFromInterfaceOrientation:(在这个方法体里面重新设置rect)
然后再次调用:
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated
*/
//UIPopoverController只能在ipad设备上面使用;作用是用于显示临时内容,特点是总是显示在当前视图最前端,当单击界面的其他地方时自动消失。
UIPopoverController *popover = [[UIPopoverController alloc]initWithContentViewController:picker];
self.imagePicker = popover;
//permittedArrowDirections 设置箭头方向
[self.imagePicker presentPopoverFromRect:CGRectMake(0, 0, 300, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[picker release];
[popover release];

点击相册中的图片 货照相机照完后点击use 后触发的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary
*)info

点击cancel 调用的方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式