1

我在一个视图控制器中有两个图像,它们已被选中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是 aUIImageView并在 中声明TypeTextViewController.h,但是当我单击按钮时,图像不会出现在TypeTextViewController.

哪里有问题?

4

3 回答 3

1

您想创建NSMutableArray,然后将这两个图像添加到此数组中。然后您必须将此 NSMutableArray 传递给另一个视图控制器。

希望这会有所帮助。

于 2014-12-24T06:53:04.383 回答
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;
  }   

}
于 2013-11-13T07:11:37.707 回答
0

// 将 Image 一个视图传递给另一个视图:

      - (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];
 }];
     }
于 2017-06-02T06:53:34.020 回答