IOS 使用照相机和图片库

来源:互联网 发布:mac文件复制到移动硬盘 编辑:程序博客网 时间:2024/04/28 12:54

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

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];
}

原创粉丝点击