UIPickerViewController 打开相机 图库 相册

iOS 获取图片有三种方法:

1. 直接调用摄像头拍照

2. 从相册中选择

3. 从图库中选择

 self.picker = [[UIImagePickerController alloc] init];
    //    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //    picker.sourceType = sourceType;  // 后边经赋值
    self.picker.delegate = self;
    self.picker.allowsEditing = YES; //是否可编辑
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择头像" message:@"从..选择" preferredStyle:UIAlertControllerStyleActionSheet];

    // 先判断支不支持
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        NSLog(@"支持相机");
        UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"相机拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:self.picker animated:YES completion:nil];
        }];
        [alert addAction:action0];
        
    }
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        NSLog(@"支持图库");
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"从图库中选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:self.picker animated:YES completion:nil];
        }];
        [alert addAction:action1];
        
    }
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
        NSLog(@"支持相片库");
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            [self presentViewController:self.picker animated:YES completion:nil];
        }];
        [alert addAction:action2];
        
    }
    
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];

    [alert addAction:action3];
  
    [self presentViewController:alert animated:YES completion:nil];
</pre><p>协议 声明</p><p></p><pre name="code" class="objc">@interface ViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) UIImagePickerController *picker;

@end

实现两个委托方法

#pragma mark -选择完成
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    //判断媒体资源类型,(图片或者是视频)
    NSString *imageType = [info objectForKey:UIImagePickerControllerMediaType];
    UIImage *gerenimage = nil;
    
    //如果是图片资源
    if ([imageType isEqualToString:@"public.image"]) {
        gerenimage = [info objectForKey:UIImagePickerControllerOriginalImage];

    }
//    //取到从系统图片库选择的图片或者照相机拍摄的图片
//    if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
//        gerenimage = [info objectForKey:UIImagePickerControllerOriginalImage];
//        
//    }else if(picker.sourceType == UIImagePickerControllerSourceTypeCamera){
//        gerenimage = [info objectForKey:UIImagePickerControllerEditedImage];
//    }
//    UIImageOrientation imageOrientation=gerenimage.imageOrientation;
//    if(imageOrientation!=UIImageOrientationUp)
//    {
//        // 原始图片可以根据照相时的角度来显示,但UIImage无法判定,于是出现获取的图片会向左转90度的现象。
//        // 以下为调整图片角度的部分
//        UIGraphicsBeginImageContext(gerenimage.size);
//        [gerenimage drawInRect:CGRectMake(0, 0, gerenimage.size.width, gerenimage.size.height)];
//        gerenimage = UIGraphicsGetImageFromCurrentImageContext();
//        UIGraphicsEndImageContext();
//        // 调整图片角度完毕
//    }
    
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 300, 150, 150)];
    imageView.image = gerenimage;
    [self.view addSubview:imageView];
    
    // 转成二进制
   // NSData * data = UIImageJPEGRepresentation(gerenimage, 0.001); // 压缩JPEG图片(图片 压缩系数)

    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma makr -取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值