在iOS设备(iPhone/iPad)中调用摄像头进行全屏拍摄时的处理方案

原创 2013年12月04日 14:54:35

在实际开发中,UIImagePickerController的调用在iPhone和iPad的中是有些许差异的,因为两者设备间的固有的的差距,在iPhone中调用照片库中的图片可以直接进行

首先应当遵循UIImagePickerControllerDelegate协议

UIImagePickerController * picker = [[UIImagePickerController alloc]init];
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.delegate = self;
        [picker setAllowsEditing:YES];
        
    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Error accessing photo library!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

调用即可

但是在iPad开发中由于屏幕大,如果是在一个小的View中调用需要遵循UIImagePickerControllerDelegate,UIPopoverControllerDelegate三个协议,并通过UIPopoverControllerView调用UIImagePickerControlle

UIImagePickerController * picker = [[UIImagePickerController alloc]init];
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.delegate = self;
        [picker setAllowsEditing:YES];
        UIPopoverController * popver = [[UIPopoverController alloc]initWithContentViewController:picker];
        self.popverController = popver;
        [self.popverController setPopoverContentSize:CGSizeMake(300, 500)];
        [self.popverController presentPopoverFromRect:CGRectMake(103, 30, 59, 59) inView:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        [popver release];
    }
    else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Error accessing photo library!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

当需要调用摄像头时,如果需要的是非全屏的调用那么跟调用照片库的区别不大

//以浮动窗口的形式打开摄像头
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController * picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.allowsEditing = YES;
        picker.modalPresentationStyle = UIModalPresentationCurrentContext;
        UIPopoverController * popver = [[UIPopoverController alloc]initWithContentViewController:picker];
        self.popverController = popver;
        picker.videoQuality = UIImagePickerControllerQualityTypeIFrame1280x720;
        picker.cameraViewTransform = CGAffineTransformScale(picker.cameraViewTransform, 0.8, 1.0);
        [self.popverController setPopoverContentSize:CGSizeMake(500, 800)];
        [self.popverController presentPopoverFromRect:CGRectMake(103, 30, 59, 59) inView:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        [popver release];
    }else{
    
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"错误" message:@"拍照功能暂时无法使用" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }

当然,如果大部分情况下使用摄像头进行拍摄时,都喜欢使用全屏,不但看起来比较酷,在苹果官方API中也是提倡的

首先,如果你调用摄像头的语句没有写在ViewController中,那么需要通过代理或者通知中心将事件转到ViewController中然后遵循UIImagePickerControllerDelegate,UINavigationControllerDelegate两个协议

#pragma mark - selectCamera
-(void)selectCamera{
    [self startCameraControllerFromViewController: self
                                    usingDelegate: self];
}

- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller
                                   usingDelegate: (id <UIImagePickerControllerDelegate,
                                                   UINavigationControllerDelegate>) delegate {
    
    if (([UIImagePickerController isSourceTypeAvailable:
          UIImagePickerControllerSourceTypeCamera] == NO)
        || (delegate == nil)
        || (controller == nil))
        return NO;
    
    
    UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
    cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    // Displays a control that allows the user to choose picture or
    // movie capture, if both are available:
    cameraUI.mediaTypes =
    [UIImagePickerController availableMediaTypesForSourceType:
     UIImagePickerControllerSourceTypeCamera];
    
    // Hides the controls for moving & scaling pictures, or for
    // trimming movies. To instead show the controls, use YES.
    cameraUI.allowsEditing = YES;
    
    cameraUI.delegate = delegate;
    
    [controller presentModalViewController: cameraUI animated: YES];
    return YES;
}

同时应当事先协议中的两个函数

- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker {
    //点击取消按钮时要实现的功能
    [picker  dismissModalViewControllerAnimated: YES];
    [picker release];
}
- (void) imagePickerController: (UIImagePickerController *) picker
 didFinishPickingMediaWithInfo: (NSDictionary *) info {
}//点击选取的图片后应当实现的内容,info中包含有选取图片的内容,通过其中的内容可以获取选择的照片

相关文章推荐

ios 相机 相册 UIImagePickerController详解

转载自:http://blog.csdn.net/kingsley_cxz/article/details/9157093 1.UIImagePickerController的静态方法: ...

iPad UIPopoverController弹出窗口的位置和坐标

TodoViewController *contentViewController = [[TodoViewController alloc] init];     UINavigationCo...

用UIImagePickerViewController自定义相机界面

原文地址:用UIImagePickerViewController自定义相机界面作者:花逝无痕 1.设定imagePacker参数   // Transform va...

Web调用安卓,苹果手机摄像头,本地图片和文件

由于要给一个客户做一个记账WAP,里面有调用手机拍照功能,这里记录一下,以供需要的朋友,下面是完整的一个HTML页面内容,放在服务器上然后浏览就可以了,只支持Chrome和Safari核的浏览器,我测...

iOS 对iphone和 ipad的摄像头和图片库的区别处理代码

iPhone跟 iPad对摄像头和图片库的代码处理有点不一样,iPad主要是用使用 UIPopoverController来包含 UIImagePickerController. ...

UIImagePickerController全屏

UIImagePickerController的默认比例是4:3,如果想要全屏的话,要对应的设置transformSwiftlet screenSize = UIScreen.mainScreen()...

iPad UIPopoverController弹出窗口的位置和坐标

TodoViewController *contentViewController = [[TodoViewController alloc] init];     UINavigationCo...

【iOS开发-107】UISplitViewController:如何取得master和detail控制器,核心在于如何运用代理

【iOS开发-107】UISplitViewController:如何取得master和detail控制器,核心在于如何运用代理...

各种iOS设备(iphone,itouch,ipad)通过自己架设的vpn服务器上网,抛开代理,使用VPN吧。

各种iOS设备(iphone,itouch,ipad)通过自己架设的vpn服务器上网,抛开代理,使用VPN吧。 测试使用的设备:windows XP计算机一台 + iPad 可用场所:比如,在学校生活...

[iOS]设置Xcode 4同时支持旧版iOS设备(armv6),并兼容iPhone和iPad

Xcode 4的Architectures设置默认只有armv7,也就是新的iOS设备,包括iPhone 3GS、iPhone 4、iPhone 4s、iPad、iPad 2等,而iPhone、i...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:在iOS设备(iPhone/iPad)中调用摄像头进行全屏拍摄时的处理方案
举报原因:
原因补充:

(最多只允许输入30个字)