【发布时间】: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];
}
请帮忙!!
【问题讨论】:
标签: ios objective-c iphone ipad