我在一个视图控制器中有两个图像,这些图像是使用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
中。
哪里有问题?
答案 0 :(得分:1)
您想要创建 NSMutableArray ,然后将两个图像添加添加到此Array.And然后您必须将此NSMutableArray传递给另一个视图控制器。
希望这有用。
答案 1 :(得分:0)
我不认为您可以将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;
}
}
答案 2 :(得分:0)
//将图像一个视图传递给其他视图:
- (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];
}];
}