1

我是使用 UIImagePicker 的新手,所以请耐心等待。基本上,我定义了两个 UIButtons 来访问相机胶卷,一旦选择了图像,图像就会出现在我的 UIImageView 中。问题是当我从其中一个按钮中选择一个图像时,它出现在两个 UIImageViews 中,而不是一个。我基本上希望能够通过两个不同的按钮选择两张不同的照片。这是编码:

-(IBAction)getPhoto1:(id)sender {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    popover1 = [[UIPopoverController alloc] initWithContentViewController:picker];
    [popover1 presentPopoverFromRect:CGRectMake(0.0, 0.0, 400., 400.0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}

-(IBAction)getPhoto2:(id)sender {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    popover2 = [[UIPopoverController alloc] initWithContentViewController:picker];
    [popover2 presentPopoverFromRect:CGRectMake(0.0, 0.0, 400.0, 400.0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}

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

    [picker dismissViewControllerAnimated:YES completion:nil];
    photo1.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    photo2.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

}
4

1 回答 1

2

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info每当您完成选择图像时调用。

当您完成从 中选择图像时UIImagePickerController,您将两者都设置UIImageView为返回的图像。

您需要区分按下了哪个按钮 - 也许您可以使用 aint来确定按下了哪个按钮。

For example, create an 'int' called buttonPressed that'll be set to 1 when getPhoto1 was pressed, or 2 when getPhoto2 was pressed. Then, in didFinishPickingMediaWithInfo, you could have:

if (buttonPressed == 1) {
    photo1.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
} else if (buttonPressed == 2) {
    photo2.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}
于 2013-03-29T19:50:32.407 回答