0

我在同一个视图中有两个 UIImageView。对于每个 imageView,我应该分配一张从相机或照片库中拍摄的照片。当我开始这样做时,我会为两个 imageView 获得相同的照片。

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

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

} 


-(IBAction)addPhoto:(id)sender{

    UIActionSheet *actionSheet =[[UIActionSheet alloc]initWithTitle:@"" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"Choose Photo" otherButtonTitles:@"Take Photo ", nil];


    [actionSheet showInView:self.view];

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    shouldUpdateFirstImage =YES;
    // chosenImage = info[UIImagePickerControllerEditedImage];
    [picker dismissViewControllerAnimated:YES completion:nil];

  if (shouldUpdateFirstImage) {
        imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        shouldUpdateFirstImage = NO;
    }
    else {
        pictureView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

        shouldUpdateFirstImage = YES;
    }

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Put Picture"])
    {
        alrtView.hidden = YES;
        pictureView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)];
       [self.view addSubview:pictureView];
        [self addPhoto:self];
    }

和 viewController.h

@interface ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIActionSheetDelegate,UITextViewDelegate>
{

    BOOL shouldUpdateFirstImage;

}

第一个 ImageView 是背景。通过 AlertView,我选择将图片添加到第一个 ImageView。之后,我回忆起相同的操作表来选择拍摄的照片。

4

2 回答 2

0

尝试这个

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
      [picker dismissViewControllerAnimated:YES completion:nil];

 if(imageView.image)
    imageView2.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
 else
    imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

}
于 2013-11-10T16:53:51.680 回答
0

TL;DR 您需要跟踪类的状态,并让您的委托方法相应地响应该状态。

听起来您需要您的班级跟踪已显示的图像数量,并根据该信息加载新的选择。我们称之为“状态”,您必须为您的 imagePickerController 委托方法实现逻辑以适当地响应该状态。

一种可能的方法是将 BOOL 范围限定为您的类(ivar 或属性都可以,但 ivar 可能更合适),然后使用它的值来跟踪应该更新哪个 imageView。

假设您已经声明了一个名为 shouldUpdateFirstImage 的 BOOL,您可以执行以下操作:

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

[picker dismissViewControllerAnimated:YES completion:nil];

if (shouldUpdateFirstImage) {
        imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        shouldUpdateFirstImage = NO;
    }
else {
    imageView2.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    shouldUpdateFirstImage = YES;
    }  
}

这将在每次连续选择时交替更新哪个 imageView。

于 2013-11-10T16:42:14.587 回答