iOS开发-iOS调用相机调用相册【将图片保存到本地相册】_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > iOS开发-iOS调用相机调用相册【将图片保存到本地相册】

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

 2017/11/8 15:54:12  小圭哥  程序员俱乐部  我要评论(0)
  • 摘要:设置头部代理<UINavigationControllerDelegate,UIImagePickerControllerDelegate>1.调用相机检测前置摄像头是否可用-(BOOL)isFrontCameraAvailable{return[UIImagePickerControllerisCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];}检测后置摄像头是否可用-(BOOL
  • 标签:图片 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{
        ///图片未能保存到本地
    }
}

 

发表评论
用户名: 匿名