美文网首页iOS DeveloperiOS 开发
iOS 相机基础(一)系统相机、相册

iOS 相机基础(一)系统相机、相册

作者: 圆脸黑猫警长 | 来源:发表于2016-09-18 11:40 被阅读419次

获取系统相机、相册用法十分简单,直接上代码,分分钟学会。

在某ViewController中直接调用如下代码即可

1.使用前应先判断是否可用
//照相机是否可用
BOOL isCameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
//相册是否可用
BOOL isPhotoLibraryAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];

2.初始化UIImagePickerController
UIImagePickerController *picker = [[UIImagePickerController alloc] init];

3.设置picker的类型(只能设置一个)
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//设置为此即打开相册
picker.sourceType = UIImagePickerControllerSourceTypeCamera;//设置为此即打开相机

4.设置回调代理(用于选中图片或者拍照完成后回图片的各种信息)
//注意:此处需要遵循两个代理协议
//UIImagePickerControllerDelegate 用于选择照片或拍照后回调
//UINavigationControllerDelegate 在相机/相册选择中会有Nav进行跳转,此代理可以用于监听事件
<UINavigationControllerDelegate, UIImagePickerControllerDelegate>   
picker.delegate = self;

5.弹出相机/相册(一定要使用modal形式才可弹出)
[self presentViewController:picker animated:YES completion:nil];

6.实现代理监听相机/相册回调
#pragma mark - UIImageViewPickerDelegate 相机/相册 回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    //选择完成后dismiss选择控制器,同时处理选择的图
    [picker dismissViewControllerAnimated:YES completion:^
    {
        //info中有选中图片的全部信息,根据需要去获取,此处获取的为原图
        UIImage *choiceImage = [info objectForKey:UIImagePickerControllerOriginalImage];
     
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
        {
            //若为相机拍摄则保存到相册
            UIImageWriteToSavedPhotosAlbum(choiceImage, NULL, NULL, NULL);
        }
    }];
}

补充:
1.picker获取前置摄像头 (默认为后置UIImagePickerControllerCameraDeviceRear
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;

2.对选中图片进行编辑
picker.allowsEditing = YES;设置后选中图片则可以进行区域编辑

3.以上属性需要在present之前设置有效

相关文章

网友评论

    本文标题:iOS 相机基础(一)系统相机、相册

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