iOS相机, 相册, 图片库 和 UIAlertController的使用

签两个代理

<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

创建UIImagePickerController对象

/* 创建对象 */
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    /* 照片是否可以编辑 */
    picker.allowsEditing = YES;
    picker.delegate = self;
 /* 根据照片来源确定AlertAction */
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
 /* 判断相册是否可以访问 */
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"从相册获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:picker animated:NO completion:^{}];

        }];

        [alert addAction:action1];
    }
 /* 判断相机是否可用 */
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"相机" style:0 handler:^(UIAlertAction *action) {

        }];

        [alert addAction:action2];

    }
 /* 判断图片库是否可以访问 */
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {

        UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"从图片库获取" style:0 handler:^(UIAlertAction *action) {

            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            [self presentViewController:picker animated:NO completion:^{}];

        }];
        [alert addAction:action3];

    }

设置取消按键推出界面

UIAlertAction *cancel  =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

    }];
    [alert addAction:cancel];

    [self presentViewController:alert animated:YES completion:^{

    }];

调用协议方法选取照片使用

/* 当点击选按钮之后, 调用协议方法 */
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    /* info参数是个字典, 要得到编辑过的image对象 */
    UIImage *image = info[@"UIImagePickerControllerEditedImage"]; /**< 字典中获取对象的字面量写法 */

    /* 将选取的照片赋给Button, 同时赋值给model类 */
    [self.addC.button setBackgroundImage:image forState:UIControlStateNormal];
    /* 返回界面 */
    [picker dismissViewControllerAnimated:YES completion:^{}];
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值