关闭

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

标签: ios开发iOSapiios设备摄像头
3082人阅读 评论(0) 收藏 举报
分类:

在实际开发中,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中包含有选取图片的内容,通过其中的内容可以获取选择的照片

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:78616次
    • 积分:1041
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:11篇
    • 译文:0篇
    • 评论:1条
    最新评论