- (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:^{}];
}