我正在为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];
}
请帮助!!
答案 0 :(得分:0)
请尝试此代码
-(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];
}
答案 1 :(得分:0)
你可以得到这样的按钮索引, 在动作表的委托方法中,
-(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;
}
}
我希望它对你有所帮助。