首先要采用三个协议,分别是 UINavigationControllerDelegate , UIImagePickerControllerDelegate , UIActionSheetDelegate
-(IBAction)photoClick:(id)sender {UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"图库", nil];[actionSheet showInView:self.view];
}- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {if (buttonIndex == 0) {//拍照//资源类型为照相机UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;//判断是否有相机if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {UIImagePickerController *picker = [[UIImagePickerController alloc] init];picker.delegate = self;//资源类型为照相机picker.sourceType = sourceType;[self presentViewController:picker animated:YES completion:nil];} else {UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"该设备无摄像头" delegate:self cancelButtonTitle:@"取消" otherButtonTitles: nil];[alertView show];}} else if (buttonIndex == 1) {//图库UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;pickerController.delegate = self;//设置选择后的图片可被编辑pickerController.allowsEditing = NO;[self presentViewController:pickerController animated:YES completion:nil];}
}-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {NSString *type = [info objectForKey:UIImagePickerControllerMediaType];//当选择的类型是图片if ([type isEqualToString:@"public.image"]) {UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];UIImageView *tempImage = [[UIImageView alloc] initWithImage:image];tempImage.frame = CGRectMake(20+picWidth+20, topImgHeight+timeImgHeight+21, UISCREEN_WIDTH-(20+picWidth+20)-20, UISCREEN_HEIGHT/2-topImgHeight-timeImgHeight-50);tempImage.userInteractionEnabled = NO;[self.view addSubview:tempImage];}[picker dismissViewControllerAnimated:YES completion:nil];
}