使用UIImagePickerController进行多个uiimageviews。 (使用多个UIButton)

时间:2012-07-29 05:05:16

标签: iphone xcode uiimageview uibutton uiimagepickercontroller

我有2个uiimage视图(imageview& imageview2)和2个uibuttons(useCameraRoll& useCameraRoll2)我已经实现了使用2个按钮在这些视图上设置图像的代码。

imageview与其按钮useCameraRoll一起正常工作,但是当我使用useCameraRoll2设置图像时,它会更改imageview的图像而不是imageview2。

观看代码/按钮1:

- (IBAction) useCameraRoll: (id)sender
{
    if ([self.popoverController isPopoverVisible]) {
        [self.popoverController dismissPopoverAnimated:YES];
    } else {
        if ([UIImagePickerController isSourceTypeAvailable:
             UIImagePickerControllerSourceTypeSavedPhotosAlbum])
        {
            UIImagePickerController *imagePicker =
            [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.sourceType =
            UIImagePickerControllerSourceTypePhotoLibrary;
            imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                      (NSString *) kUTTypeImage,
                                      nil];
            imagePicker.allowsEditing = NO;

            self.popoverController = [[UIPopoverController alloc]
                                      initWithContentViewController:imagePicker];

            self.popoverController.delegate = self;

            [self.popoverController presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

            newMedia = NO;
        }
    }
}





#pragma mark -
#pragma mark UIImagePickerControllerDelegate

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


{
    [self.popoverController dismissPopoverAnimated:true];
    NSString *mediaType = [info
                           objectForKey:UIImagePickerControllerMediaType];
   // [self dismissModalViewControllerAnimated:YES];
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = [info
                          objectForKey:UIImagePickerControllerOriginalImage];

        self.imageView.image = image;
        if (newMedia)
            UIImageWriteToSavedPhotosAlbum(image,
                                           self,  
                                           @selector(image:finishedSavingWithError:contextInfo:),
                                           nil);
    }
    else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
    {
        // Code here to support video if enabled
    }
}

视图代码/按钮2:

- (IBAction) useCameraRoll2: (id)sender
{
    if ([self.popoverController2 isPopoverVisible]) {
        [self.popoverController2 dismissPopoverAnimated:YES];
    } else {
        if ([UIImagePickerController isSourceTypeAvailable:
             UIImagePickerControllerSourceTypeSavedPhotosAlbum])
        {
            UIImagePickerController *imagePicker2 =
            [[UIImagePickerController alloc] init];
            imagePicker2.delegate = self;
            imagePicker2.sourceType =
            UIImagePickerControllerSourceTypePhotoLibrary;
            imagePicker2.mediaTypes = [NSArray arrayWithObjects:
                                      (NSString *) kUTTypeImage,
                                      nil];
            imagePicker2.allowsEditing = NO;

            self.popoverController2 = [[UIPopoverController alloc]
                                      initWithContentViewController:imagePicker2];

            self.popoverController2.delegate = self;



            [self.popoverController2 presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

            newMedia = NO;
        }
    }
}


#pragma mark -
#pragma mark UIImagePickerControllerDelegate

-(void)imagePickerController2:(UIImagePickerController *)picker2
didFinishPickingMediaWithInfo:(NSDictionary *)info2
{
    [self.popoverController2 dismissPopoverAnimated:true];
    NSString *mediaType2 = [info2
                           objectForKey:UIImagePickerControllerMediaType];
    // [self dismissModalViewControllerAnimated:YES];
    if ([mediaType2 isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image2 = [info2
                          objectForKey:UIImagePickerControllerOriginalImage];

        self.imageView2.image = image2;
        if (newMedia2)
            UIImageWriteToSavedPhotosAlbum(image2,
                                           self,  
                                           @selector(image:finishedSavingWithError:contextInfo:),
                                           nil);
    }
    else if ([mediaType2 isEqualToString:(NSString *)kUTTypeMovie])
    {
        // Code here to support video if enabled
    }
}

我已检查过界面构建​​器中的所有连接是否都连接到右视图。

有人知道为什么会出错吗

此致

1 个答案:

答案 0 :(得分:4)

我认为您的问题可能来自您的委托:您的2 UIImagePickerController使用相同的委托“self”。以下方法:

-(void)imagePickerController2:(UIImagePickerController *)picker2 didFinishPickingMediaWithInfo:(NSDictionary *)info2

实际上从未被调用过,并且不是UIImagePickerControllerDelegate的有效声明。

您需要区分哪个操作位于选择器的原点,并在单个委托方法的代码中相应地更新正确的图像。

也许你可以创建一个实例变量:

UIImageView *_currentPickerTarget

然后在每种情况下呈现选择器之前设置此变量:

- (IBAction) useCameraRoll: (id)sender {
    (...)
    _currentPickerTarget = imageView;
}

- (IBAction) useCameraRoll2: (id)sender {
    (...)
    _currentPickerTarget = imageView2;
}

然后在您的委托方法中,您只需通过替换:

来更新_currentPickerTarget
self.imageView.image = image;

通过

_currentPickerTarget.image = image;

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info函数中。

希望这会有所帮助。 克里斯托夫