如何将从 UIImagePickerController 挑选的两个图像传递给另一个 ViewController
Posted
技术标签:
【中文标题】如何将从 UIImagePickerController 挑选的两个图像传递给另一个 ViewController【英文标题】:How to pass two images picked from UIImagePickerController to another ViewController 【发布时间】:2013-11-13 06:31:38 【问题描述】:我在一个视图控制器中有两张图片,它们是用UIImagePickerController
挑选的:
ChoosePhotosViewController.h
@interface PickPhotosViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *pickedImage1; // image 1
@property (strong, nonatomic) IBOutlet UIImag8eView *pickedImage2; // image 2
- (IBAction)pickImage1:(id)sender; // button that picks up the first image
- (IBAction)pickImage2:(id)sender; // button that picks up the seconds image
- (IBAction)nextButton:(id)sender;
在实现文件中 - ChoosePhotosViewController.m
@implementation PickPhotosViewController
@synthesize pickedImage1, pickedImage2;
我为每个按钮编写了一个代码,该代码通过一系列操作调用操作表:
-(IBAction)pickImage1:(id)sender
[[NSUserDefaults standardUserDefaults] setInteger:CurrentImageCategoryImage1 forKey:@"currentImageCategory"];
[[NSUserDefaults standardUserDefaults] synchronize];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose image one" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Choose existing photo", @"Take new photo", nil];
[actionSheet showInView:self.view];
有两个动作 - 从相机空中拍摄照片从库上传:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
if (buttonIndex == 0)
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
else if (buttonIndex == 1)
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
第二个按钮的算法类似。然后在我写的UIImagePickerController
方法中;
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
if([[NSUserDefaults standardUserDefaults] integerForKey:@"currentImageCategory"] ==CurrentImageCategoryImage1)
self.pickedImage1.image = chosenImage;
else if([[NSUserDefaults standardUserDefaults] integerForKey:@"currentImageCategory"] ==CurrentImageCategoryImage2)
self.pickedImage2.image = chosenImage;
然后,当我单击“下一步”按钮时,我需要在下一个 viewController 中显示这两个图像。我使用故事板,这就是我写prepareForSegue
方法的原因:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
if ([segue.identifier isEqualToString:@“next"])
TypeTextViewController *transferedImage = [segue destinationViewController];
transferedImage.imageWithText1 = pickedImage1;
其中imageWithText1
是UIImageView
并在TypeTextViewController.h
中声明,但是当我单击按钮时,图像不会出现在TypeTextViewController
中。
哪里出了问题?
【问题讨论】:
你的引号不正确,把@"next"。你检查是否阻止工作? 在代码中标记是好的,也许当我在 *** 上输入时我做错了。代码正在运行 【参考方案1】:你想创建NSMutableArray,然后将这两个Images添加到这个数组中。然后你必须把这个NSMutableArray传递给另一个视图控制器。
希望这会有所帮助。
【讨论】:
【参考方案2】:我认为您不能将 UIImageView 从一个 VC 传递给另一个 VC。我认为我们只能传递 UIImage 而不能传递 UIImageView。试试这个。。但我不确定。
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
if ([segue.identifier isEqualToString:@“next"])
TypeTextViewController *transferedImage = [segue destinationViewController];
transferedImage.imageWithText1.image = pickedImage1.image;
【讨论】:
它不起作用。这是否意味着我应该在第二个 VC 中创建 UIImage 属性来传递图像?【参考方案3】:// 将图像从一个视图传递到另一个视图:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
[self dismissViewControllerAnimated:YES completion:^
color_ViewController*secondview=[[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"color_ViewController"];
secondview.b_image=image;
[[NSUserDefaults standardUserDefaults ]setInteger:1 forKey:@"123"];
[[self navigationController] pushViewController:secondview animated:YES];
];
【讨论】:
以上是关于如何将从 UIImagePickerController 挑选的两个图像传递给另一个 ViewController的主要内容,如果未能解决你的问题,请参考以下文章