UIImagePickerController调用相册/相机
UIImagePickerController调用相册/相机
海二少 发表于2年前
UIImagePickerController调用相册/相机
  • 发表于 2年前
  • 阅读 30
  • 收藏 4
  • 点赞 2
  • 评论 0

移动开发云端新模式探索实践 >>>   

摘要: UIImagePickerController
  - (IBAction)GetPhoto:(id)sender
{
    UIActionSheet *sheet;
    // 判断是否支持相机
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"从相册选择" otherButtonTitles:@"拍照",@"取消", nil];
    }
    else
    {
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"从相册选择" otherButtonTitles:@"取消", nil];
    }
    [sheet showInView:self.view];
    
}

// 添加图片
#pragma mark -------UIActionSheetDelegate
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSUInteger sourceType = 0;
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        switch (buttonIndex)
        {
            case 0:
                // 从相册中选取照片
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                break;
            case 1:
                // 从摄像头选取照片
                sourceType = UIImagePickerControllerSourceTypeCamera;
                break;
                
            case 2:
                // 取消
                return;
        }
    }
    else
    {
        // 显示所有的照片
        sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    }
    
    // 跳转到相机或相册页面
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = sourceType;
    // 设置为YES,表示 允许用户编辑图片,否则,不允许用户编辑
    picker.allowsEditing = NO;
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.img.image = image;
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}

// 用户选择取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
}  

 

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 86
码字总数 48854
×
海二少
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: