-1

使用两个 UIImageview 和两个按钮时点击第一个按钮图像选择器让您选择图像

从照片库和第二个按钮相同。这就是我所做的

视图控制器.h

UIImagePickerController *pic1;
UIImagePickerController *pic2;
UIImage *image1;
UIImage *image2;

IBOutlet UIImageView *imageview1;
IBOutlet UIImageView *imageview2;

-(IBAction)Load1:(id)sender;
-(IBAction)Load2:(id)sender;

vewcontroller.m

  -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

   image1 = [info objectForKey:UIImagePickerControllerOriginalImage];
   image2 = [info objectForKey:UIImagePickerControllerOriginalImage];
   [imageview1 setImage:image1];
   [imageview2 setImage:image2];


   [self dismissViewControllerAnimated:YES completion:NULL];

}


-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
   [self dismissViewControllerAnimated:YES completion:NULL];
}


- (IBAction)Load1:(id)sender
{
   pic1 = [[UIImagePickerController alloc]init];
   pic1.delegate = self;
   [pic1 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
   [self presentViewController:pic1 animated:YES completion:NULL];
}

- (IBAction)Load2:(id)sender
{
   pic2 = [[UIImagePickerController alloc]init];
   pic2.delegate = self;
   [pic2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
   [self presentViewController:pic2 animated:YES completion:NULL];


}

当我跑的时候。第一个按钮工作。让您从照片库中选择图像并显示它,第二个按钮照常工作,但您选择的图像同时显示在 UIImageview

我在这里做错了什么,谢谢

4

1 回答 1

1

这些行导致两个图像视图具有您选择的相同图像:

image1 = [info objectForKey:UIImagePickerControllerOriginalImage];
image2 = [info objectForKey:UIImagePickerControllerOriginalImage];

两个图像视图都具有与 : in 相同的源图像UIImagePickerControllerOriginalImage

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

您需要做的是区分按下了哪个按钮以及该图像应包含在哪个图像视图中。当然可以遵循许多方法:就像您可以使用一些 bool 来区分调用者,例如:

 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   if (button1Clicked)   //Boolean to determine which button was pressed, set it in IBAction of the button
   {
      image1 = [info objectForKey:UIImagePickerControllerOriginalImage];
      [imageview1 setImage:image1];
   }
   else
   {
      image2 = [info objectForKey:UIImagePickerControllerOriginalImage];
      [imageview2 setImage:image2];
   }

      [self dismissViewControllerAnimated:YES completion:NULL];

}

或任何其他相关方法。

于 2013-08-11T11:57:51.773 回答