码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2017-11-08 17:49:25      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:init   navig   picker   sed   object   取消   ogre   nav   sel   

设置头部代理

<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调用相机调用相册【将图片保存到本地相册】

标签:init   navig   picker   sed   object   取消   ogre   nav   sel   

原文地址:http://www.cnblogs.com/wangkejia/p/7804279.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!