IOS开发中使用照相机功能的实现

本文介绍如何在程序中通过ActionSheet提供选项,让用户选择是使用相机拍摄照片还是从相册中选择照片,并详细阐述了实现过程,包括初始化UIImagePickerController对象、设置Delegate和处理用户选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在程序中使用照相机,或者从相册中选择需要的照片,可以按照以下的步骤实现。

1.生成一个UIImagePickerController对象
2.用presentModalViewController来显示它
3.实现UIImagePickerControllerDelegate的方法

举个例子,此例子的功能如下:
点击一个照相按钮,弹出一个ActionSheet让用户选择是从相册选择照片还是用相机新照一张照片。

代码如下:
1.点击照相按钮后弹出ActionSheet

- (void)takePhotoBtnTapped:(UIButton *)sender {
  //Show Action Sheet: 1. Take Photo 2. Select Photo From Album
  UIActionSheet *photoBtnActionSheet =
  [[UIActionSheet alloc] initWithTitle:nil
                              delegate:self
                     cancelButtonTitle:@"Cancel"
                destructiveButtonTitle:nil
                     otherButtonTitles:@"Photo Library",@"Take Photo", nil];
  [photoBtnActionSheet setActionSheetStyle:UIActionSheetStyleBlackOpaque];
  [photoBtnActionSheet showInView:[self.view window]];
}

2. 在header里添加以下三个Delegate

UIActionSheetDelegate
UIImagePickerControllerDelegate
UINavigationControllerDelegate

3.UIActionSheetDelegate的实现

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  NSLog(@"Action Sheet Button Index: %d",buttonIndex);
  if (buttonIndex == 0) {
    //Show Photo Library
    @try {
      if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
        UIImagePickerController *imgPickerVC = [[UIImagePickerController alloc] init];
        [imgPickerVC setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [imgPickerVC.navigationBar setBarStyle:UIBarStyleBlack];
        [imgPickerVC setDelegate:self];
        [imgPickerVC setAllowsEditing:NO];
        //显示Image Picker
        [self presentModalViewController:imgPickerVC animated:NO];
      }else {
        NSLog(@"Album is not available.");
      }
    }
    @catch (NSException *exception) {
      //Error
      NSLog(@"Album is not available.");
    }
  }
  if (buttonIndex == 1) {
    //Take Photo with Camera
    @try {
      if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *cameraVC = [[UIImagePickerController alloc] init];
        [cameraVC setSourceType:UIImagePickerControllerSourceTypeCamera];
        [cameraVC.navigationBar setBarStyle:UIBarStyleBlack];
        [cameraVC setDelegate:self];
        [cameraVC setAllowsEditing:NO];
        //显示Camera VC
        [self presentModalViewController:cameraVC animated:NO];
        
      }else {
        NSLog(@"Camera is not available.");
      }
    }
    @catch (NSException *exception) {
      NSLog(@"Camera is not available.");
    }
  }
}

4.UIImagePickerControllerDelegate的实现

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  NSLog(@"Image Picker Controller canceled.");
  //Cancel以后将ImagePicker删除
  [self dismissModalViewControllerAnimated:NO];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  NSLog(@"Image Picker Controller did finish picking media.");
  //TODO:选择照片或者照相完成以后的处理
  
  [self dismissModalViewControllerAnimated:NO];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值