iOS开发-iOS调用相机调用相册将图片保存到本地相册

Posted 小K’s Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS开发-iOS调用相机调用相册将图片保存到本地相册相关的知识,希望对你有一定的参考价值。

设置头部代理

<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

1.调用相机

检测前置摄像头是否可用

- (BOOL)isFrontCameraAvailable{
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
}

检测后置摄像头是否可用

- (BOOL)isRearCameraAvailable{
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
}

调用相机

///调用相机
- (void)persentImagePicker{
    if (!_imagePickerG) {
        ///初始化相机
        _imagePickerG = [[UIImagePickerController alloc]init];
        ///代理
        _imagePickerG.delegate = self;
    }
    
    // 前面的摄像头是否可用
    if ([self isFrontCameraAvailable]) {
        _imagePickerG.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    // 后面的摄像头是否可用
    else if ([self isFirstResponder]){
        _imagePickerG.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    else{
        [SVProgressHUD showErrorWithStatus:@"没有相机可用~"];
        return;
    }
    ///允许拍照后裁剪
    _imagePickerG.allowsEditing = YES;
    [self.navigationController presentViewController:_imagePickerG animated:YES completion:nil];
}

2.调用相册

///调用本地相册
- (void)persentImagePicker{
    if (!_imagePickerG) {
        ///初始化相机
        _imagePickerG = [[UIImagePickerController alloc]init];
        ///代理
        _imagePickerG.delegate = self;
    }
    ///相册
    _imagePickerG.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    _imagePickerG.allowsEditing = YES;
    [self.navigationController presentViewController:_imagePickerG animated:YES completion:nil];
}

3.代理事件

///取消选择图片(拍照)
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
///选择图片完成(从相册或者拍照完成)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];///原图
    //获取修剪后的图片
    UIImage *imageUp = [info objectForKey:UIImagePickerControllerEditedImage];
}

4.将图片保存到本地

///保存图片到本地相册
-(void)imageTopicSave:(UIImage *)image{
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image: didFinishSavingWithError: contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if (error == nil) {
        
    }
    else{
        ///图片未能保存到本地
    }
}

 

以上是关于iOS开发-iOS调用相机调用相册将图片保存到本地相册的主要内容,如果未能解决你的问题,请参考以下文章

iOS开发小技巧--实现将图片保存到本地相册

Android下载图片/调用系统相机拍照显示并保存到本地

iOS开发中,如何将图片保存本地相册中

iOS开发中,获取图片之后保存或下载到本地相册中

H5调用手机相机和相册,并且将图片转化为base64的格式

安卓开发从本地相册或拍照上传图片