在 iPad 上选择照片并从相机拍摄
Posted
技术标签:
【中文标题】在 iPad 上选择照片并从相机拍摄【英文标题】:Choosing Photos and Taking from camera on iPad 【发布时间】:2015-08-12 04:34:03 【问题描述】:我对 iPhone 和 iPad xib 使用相同的 .h 和 .m 文件。我想通过点击按钮捕捉图像或从照片中进行选择。我该怎么做呢?如果我从弹出窗口中的照片中选择,我会得到照片库但没有选择照片,如果我从相机中选择,相机也不会打开!不过,在 iPhone 上一切正常。这是我正在使用的代码:
-(IBAction)getPhotos:(id)sender
self.action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Take New Photo",@"Choose From Existing",@"Remove Photo",@"Cancel", nil];
self.action.actionSheetStyle = UIActionSheetStyleAutomatic;
self.action.destructiveButtonIndex = 2;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
[self.action showInView:self.view];
else
[self.action showFromRect:self.button1.frame inView:self.view animated:YES];
-(void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex
if (buttonIndex == 0)
[self TakePhotoWithCamera];
else if (buttonIndex == 1)
[self SelectPhotoFromLibrary];
else if(buttonIndex == 2)
self.imageView.image = [UIImage imageNamed:@"no_photo.png"];
-(void) TakePhotoWithCamera
[self startCameraPickerFromViewController:self usingDelegate:self];
-(void) SelectPhotoFromLibrary
[self startLibraryPickerFromViewController:self usingDelegate:self];
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
[self dismissViewControllerAnimated:YES completion:nil];
- (BOOL)startCameraPickerFromViewController:(UIViewController*)controller usingDelegate:(id<UIImagePickerControllerDelegate>)delegateObject
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.allowsEditing = YES;
picker.delegate = self;
[controller presentViewController:picker animated:YES completion:nil];
return YES;
- (BOOL)startLibraryPickerFromViewController:(UIViewController*)controller usingDelegate:(id<UIImagePickerControllerDelegate>)delegateObject
picker = [[UIImagePickerController alloc]init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsEditing = YES;
picker.delegate = self;
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
[controller presentViewController:picker animated:YES completion:nil];
else
popover=[[UIPopoverController alloc]initWithContentViewController:picker];
[popover presentPopoverFromRect:self.imageView.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
return YES;
- (void)imagePickerController:(UIImagePickerController *)pickers didFinishPickingMediaWithInfo:(NSDictionary *)info
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
[picker dismissViewControllerAnimated:YES completion:nil];
else
[popover dismissPopoverAnimated:YES];
self.imageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
请帮忙!!
【问题讨论】:
【参考方案1】:请试试这个代码
-(IBAction)getPhotos:(id)sender
self.action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Take New Photo",@"Choose From Existing",@"Remove Photo",@"Cancel", nil];
[self.action showInView:[UIApplication sharedApplication].keyWindow];
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
switch (buttonIndex)
case 0:
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.allowsEditing = YES;
picker.delegate = self;
[self.navigationController presentViewController: picker animated: YES completion: nil];
break;
case 1:
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsEditing = YES;
picker.delegate = self;
[self.navigationController presentViewController: picker animated: YES completion: nil];
break;
case 2:
self.imageView.image = [UIImage imageNamed:@"no_photo.png"];
break;
default:
[self.navigationController dismissViewControllerAnimated: YES completion: nil];
break;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
self.imageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
[self.navigationController dismissViewControllerAnimated: YES completion: nil];
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
[self.navigationController dismissViewControllerAnimated: YES completion: nil];
// [picker dismissViewControllerAnimated:YES completion:nil];
【讨论】:
如果您喜欢我的回答,请接受并支持我的回答。【参考方案2】:你可以像这样得到按钮索引, 在您的操作表的委托方法中,
-(void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex
switch (i)
case 0:
[self TakePhotoWithCamera];
break;
case 1:
[self SelectPhotoFromLibrary];
break;
case 2;
//no image
break;
default:
break;
希望对你有帮助
【讨论】:
以上是关于在 iPad 上选择照片并从相机拍摄的主要内容,如果未能解决你的问题,请参考以下文章