我正在开发一款应用,它有两个图像视图。每个图像视图都有自己的按钮,用于从相机胶卷中选择图像。当我单击按钮时,它显示一个相机胶卷,我选择一个图像,它显示在图像视图1中,但是当我单击第二个按钮从相机胶卷中选择图像并选择图像时,它会在图像视图1上显示图像imageview2。有谁知道如何解决它???。这是我的界面。我使用的是Objective-c语言。
这是代码:
- (IBAction)CNICFront:(id)sender {
picker = [[UIImagePickerController alloc]init];
picker.delegate=self;
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)CNICBack:(id)sender {
pic = [[UIImagePickerController alloc]init];
pic.delegate=self;
pic setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:pic animated:YES completion:NULL];
}
代表方法用于图像视图1是这样的: -
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
image=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
[self.imageView1 setImage:image];
[self dismissViewControllerAnimated:YES completion:NULL];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:NULL];
}
答案 0 :(得分:0)
.h文件:
@property (weak, nonatomic) IBOutlet UIImageView *img1;
@property (weak, nonatomic) IBOutlet UIImageView *img2;
- (IBAction)but1:(UIButton *)sender;
- (IBAction)but2:(UIButton *)sender;
.m文件
#import "ViewController.h"
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
UIImagePickerController *picker;
NSString *ImageViewC;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
picker = [[UIImagePickerController alloc]init];
picker.delegate=self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)but1:(UIButton *)sender {
ImageViewC=@"1";
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)but2:(UIButton *)sender {
ImageViewC=@"2";
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker animated:YES completion:NULL];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage * image=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
if ([ImageViewC isEqualToString:@"1"]) {
[self.img1 setImage:image];
}else{
[self.img2 setImage:image];
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:NULL];
}
@end