iOS 开发调用相机以及获取相册照片功能

  //添加代理方法

@interface MineViewController () <UITableViewDelegate, UITableViewDataSource, PayCellDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate>

 

 

//定义消息框

    UIActionSheet * act =[[UIActionSheet alloc]initWithTitle:@"请选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"选择相册",@"选择相机", nil];

    //显示消息框

    [act showInView:self.view];

 

//消息框代理实现

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

    //定义图片选择器

    UIImagePickerController * picker = [[UIImagePickerController alloc]init];

    //判断

    switch (buttonIndex) {

        case 0:

            //判断系统是否允许选择 相册

            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

                //图片选择是相册(图片来源自相册)

                picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

                //设置代理

                picker.delegate=self;

                //模态显示界面

                [self presentViewController:picker animated:YES completion:nil];

                

            }

            break;

        case 1://判断系统是否允许选择 相机

            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

                //图片选择是相册(图片来源自相册)

                picker.sourceType = UIImagePickerControllerSourceTypeCamera;

                //设置代理

                picker.delegate=self;

                //模态显示界面

                [self presentViewController:picker animated:YES completion:nil];

            }

            else {

                NSLog(@"不支持相机");

            }

            

            

            break;

            

            

        default:

            break;

    }

}

 

 

//实现图片选择器代理

//参数:图片选择器  字典参数

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    //通过key值获取到图片

    UIImage * image =info[UIImagePickerControllerOriginalImage];

    NSLog(@"image=%@  info=%@",image, info);

    

    //判断数据源类型

    if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {

        

        //设置图片背景

        [userIconBtn setBackgroundImage:image forState:UIControlStateNormal];

        NSUserDefaults * user = [NSUserDefaults standardUserDefaults];

        NSData* imgData = UIImageJPEGRepresentation(image, 1);

        isPhotoChoose = YES;

        NSLog(@"第一次调用这个方法");

        [user setObject:imgData forKey:@"saveIcon"];

        [user synchronize];

 

        [self dismissViewControllerAnimated:YES completion:nil];

    }

    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {

        NSLog(@"在相机中选择图片");

        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

        

        //设置图片背景

        [userIconBtn setBackgroundImage:image forState:UIControlStateNormal];

        NSUserDefaults * user = [NSUserDefaults standardUserDefaults];

        NSData* imgData = UIImageJPEGRepresentation(image, 1);

        isPhotoChoose = YES;

        NSLog(@"第一次调用这个方法");

        [user setObject:imgData forKey:@"saveIcon"];

        [user synchronize];

        

        [self dismissViewControllerAnimated:YES completion:nil];

    }

}

 

转载于:https://www.cnblogs.com/fantasy940155655/p/5799132.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值