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