技术文章分类(180)

技术随笔(11)

IOS使用相机,选择相册功能

首先声明,由于本人mac电脑显示器暂时不能使用,以下代码是抄别人的,尚未核实,待过两天核实后这句话即被删除,并且会提供一个更优秀的版本(这个版本已然非常优秀了)

 

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

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

 

 

posted @ 2014-06-11 09:06  坤哥MartinLi  阅读(240)  评论(0编辑  收藏  举报