美文网首页
iOS 开发多媒体---相册选取和拍照

iOS 开发多媒体---相册选取和拍照

作者: 胡胡LoL | 来源:发表于2017-06-14 13:02 被阅读97次

关于 iOS 相册选取和拍照

需要注意的是需要现在plist 文件中加入以下2个属性
①.Privacy - Photo Library Usage Description
②.Privacy - Camera Usage Description
遵循2个代理方法
UIImagePickerControllerDelegate
UINavigationControllerDelegate

1.摄像头

        //是否是摄像头
        BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
        if (!isCamera) { //若不可用,弹出警告框
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"无可用摄像头" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            return;
        }
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
/**  
        *UIImagePickerControllerSourceTypePhotoLibrary  ->所有资源文件夹
         UIImagePickerControllerSourceTypeCamera        ->摄像头
         UIImagePickerControllerSourceTypeSavedPhotosAlbum ->内置相册
         */
        
//设置代理,遵循UINavigationControllerDelegate,UIImagePickerControllerDelegate协议
        imagePicker.delegate = self;
        [self presentViewController:imagePicker animated:YES completion:nil];

2.访问相册

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        imagePicker.delegate = self;
        [self presentViewController:imagePicker animated:YES completion:nil];

3.代理方法

#pragma mark - 协议方法的实现
//协议方法,选择完毕以后,显示在 cell 里面
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSLog(@"%@",info);  //UIImagePickerControllerMediaType,UIImagePickerControllerOriginalImage,UIImagePickerControllerReferenceURL
    NSString *mediaType = info[@"UIImagePickerControllerMediaType"];
    if ([mediaType isEqualToString:@"public.image"]) {  //判断是否为图片
        
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
   
        imageCell.imageView.image = image;
        
        //通过判断picker的sourceType,如果是拍照则保存到相册去
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSLog(@"已保存");
}

相关文章

网友评论

      本文标题:iOS 开发多媒体---相册选取和拍照

      本文链接:https://www.haomeiwen.com/subject/nlusqxtx.html