使用两个UIImageview和两个按钮,当点击第一个按钮图像选择器让你选择图像
从照片库和第二个按钮。这就是我做的事情
viewcontroler.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
中显示 我在这里做错了什么, 谢谢答案 0 :(得分:1)
这些行导致两个图像视图具有您选择的相同图像:
image1 = [info objectForKey:UIImagePickerControllerOriginalImage];
image2 = [info objectForKey:UIImagePickerControllerOriginalImage];
两个图像视图都具有与:UIImagePickerControllerOriginalImage
相同的源图像:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
您需要做的是区分按下哪个按钮以及应该包含该图像的imageview。当然可以遵循许多方法:就像你可以使用一些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];
}
或任何其他相关方法。