UIImagePickerController的简单使用

来源:互联网 发布:手机阿里云 编辑:程序博客网 时间:2024/05/17 09:26

#pragma mark 当点击修改头像按钮的时候,触发的方法- (IBAction)modifyUserPic:(UIButton *)sender{    myActionSheet = [[UIActionSheetalloc]initWithTitle:nil                                               delegate:self                                      cancelButtonTitle:@"取消"                                 destructiveButtonTitle:nil                                      otherButtonTitles:@"打开相机",@"从相册中选择",nil];        [myActionSheet showInView:self.view];    }#pragma mark - UIActionSheetDelegate- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{    switch (buttonIndex) {        case 0:            [self takePhoto];//打开相机            break;        case 1:            [self loadPhoto];//打开本地相册            break;    }}//开始拍照-(void)takePhoto{    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {                UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];                picker.delegate = self;                picker.allowsEditing = YES;                picker.sourceType = sourceType;                [self presentViewController:picker animated:YEScompletion:nil];    }    else    {        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil                                                       message:@"无法打开相机,请在真机中调试!"                                                      delegate:nil                                             cancelButtonTitle:@"确定"                                             otherButtonTitles:nil];        [alert show];    }    }//打开本地相册-(void)loadPhoto{    UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;        picker.delegate = self;        picker.allowsEditing = YES;        [selfpresentViewController:picker animated:YES completion:^{}];    }#pragma mark - UIImagePickerControllerDelegate- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{        _userPicImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];        [picker dismissViewControllerAnimated:YES completion:nil];    }


0 0
原创粉丝点击