iOS 开发相机,相册权限问题,代码总结

来源:互联网 发布:发那科机器人编程实例 编辑:程序博客网 时间:2024/06/01 08:00

//调用相机

 UIActionSheet *actionSheet = [[UIActionSheetalloc]initWithTitle:nildelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"拍照",@"相册",nil];

            actionSheet.actionSheetStyle = UIActionSheetStyleDefault;

            [actionSheet showInView:self.view];

/**actionSheet 代理方法*/

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    switch (buttonIndex) {

        case 0:

        {

            

            if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

                AVAuthorizationStatus authStatus = [AVCaptureDeviceauthorizationStatusForMediaType:AVMediaTypeVideo];

                if(authStatus == AVAuthorizationStatusDenied){

                    UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:nilmessage:@"请在iPhone设置-隐私-相机选项中,允许美不美访问你的相机"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil];

                    [alertView show];

                }else{

                    UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];

                    picker.delegate = self;//

                    picker.allowsEditing = YES;

                    picker.sourceType =UIImagePickerControllerSourceTypeCamera;//UIImagePicker选择器的类型,UIImagePickerControllerSourceTypeCamera调用系统相机

                    [selfpresentViewController:pickeranimated:YEScompletion:NULL];

                    

                }

                

            }else{

                //如果当前设备没有摄像头

                UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:nilmessage:@"当前设备没有摄像头。"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil];

                [alertView show];

            }

            break;

        }

        case 1:

        {

            PhotoPickerViewController *picker = [[PhotoPickerViewControlleralloc]init];

            picker.maximumNumberOfSelection =9;

            picker.assetsFilter = [ALAssetsFilterallPhotos];

            picker.showEmptyGroups=NO;

            picker.delegate=self;

            picker.selectionFilter = [NSPredicatepredicateWithBlock:^BOOL(id evaluatedObject,NSDictionary *bindings) {

                if ([[(ALAsset*)evaluatedObjectvalueForProperty:ALAssetPropertyType]isEqual:ALAssetTypeVideo]) {

                    NSTimeInterval duration = [[(ALAsset*)evaluatedObjectvalueForProperty:ALAssetPropertyDuration]doubleValue];

                    return duration >= 5;

                } else {

                    return YES;

                }

            }];

            

            [self.selectedViewControllerpresentViewController:pickeranimated:YEScompletion:NULL];

            break;

        }

            

        case 2:

        {

            break;

        }

        default:

            break;

    }

    

}

#pragma mark - ZYQAssetPickerController Delegate

-(void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)assets{

    //点击的照片 assets

//        UIStoryboard *CameraStoryBoard = [UIStoryboard storyboardWithName:@"Camera" bundle:nil];

//       CameraView = [CameraStoryBoard instantiateViewControllerWithIdentifier:@"CameraView"];

    

    UIStoryboard *TailorStoryBoard = [UIStoryboardstoryboardWithName:@"TailorImageController"bundle:nil];

    TailorController = [TailorStoryBoardinstantiateViewControllerWithIdentifier:@"TailorImageController"];

    

      //图片降质

       NSMutableArray *tempImageArray = [NSMutableArrayarray];

       for (ALAsset *assetin assets) {

           UIImage *OldImg=[UIImageimageWithCGImage:asset.defaultRepresentation.fullResolutionImage];

           NSData *tempImageData = UIImageJPEGRepresentation(OldImg, 0.4);

           UIImage *NewImage = [UIImageimageWithData:tempImageData];

           [tempImageArray addObject:NewImage];

    }

//     CameraView.photoArray = tempImageArray;

    

    TailorController.photoArray = tempImageArray;

//    [UIApplication sharedApplication].keyWindow.rootViewController = CameraViewNvc;

    [selfperformSelector:@selector(IssueViewController)withObject:nilafterDelay:0.2];

   

//    [self.selectedViewController.navigationController pushViewController:CameraView animated:NO];

    

}


//发布界面

- (void)IssueViewController

{

//     [self.selectedViewController pushViewController:CameraView animated:NO];

    

    [self.selectedViewControllerpushViewController:TailorControlleranimated:NO];

}


//相册取消

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    if ( picker.sourceType ==UIImagePickerControllerSourceTypeCamera) {

        [picker dismissViewControllerAnimated:NOcompletion:nil];

    }else{

        ZYQAssetPickerController * zyqAssetVC = [[ZYQAssetPickerControlleralloc]init];

        [self.navigationControllerpresentViewController:zyqAssetVCanimated:YEScompletion:nil];

    }


}

//相机拍照功能选区照片

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

{

   

    [picker dismissViewControllerAnimated:NOcompletion:^{

        

    }];

    

//    UIStoryboard *CameraStoryBoard = [UIStoryboard storyboardWithName:@"Camera" bundle:nil];

//    CameraView = [CameraStoryBoard instantiateViewControllerWithIdentifier:@"CameraView"];

    

    UIStoryboard *TailorStoryBoard = [UIStoryboardstoryboardWithName:@"TailorImageController"bundle:nil];

    TailorController = [TailorStoryBoardinstantiateViewControllerWithIdentifier:@"TailorImageController"];

    

    //图片降质

    UIImage *OldImg =info[UIImagePickerControllerOriginalImage];

    NSData *tempImageData = UIImageJPEGRepresentation(OldImg, 0.75);

    UIImage *NewImage = [UIImageimageWithData:tempImageData];

//    CameraView.photoArray = [NSArray arrayWithObjects:NewImage, nil];

    TailorController.photoArray = [NSArrayarrayWithObjects:NewImage,nil];

    [selfperformSelector:@selector(IssueViewController)withObject:nilafterDelay:0.2];

}



0 0