1

在我的测试应用程序中,我有两个按钮(takePhoto1 和 takePhoto2)和两个 UIImageView(photo1View 和 photo2View)。第一个按钮拍摄照片并将其显示在第一个图像视图中。第二个按钮拍摄另一张照片,并且应该在第二个图像视图中显示它。但是,由于某种原因,第二张照片显示在 photoView1 而不是 photoView2 中。

我还没有实现任何代码来保存照片,所以我不担心照片会保留在图像视图中。

我需要弄清楚的是如何确保拍摄第二张照片时,它会显示在适当的视图中。这是我到目前为止所拥有的:

//Take photo1 and display in top image view
- (void)takePhoto1;
{
    [self dismissViewControllerAnimated:YES completion:NULL];

    if ([self.capturedImages count] > 0)
    {
        if ([self.capturedImages count] == 1)
        {
            // Camera took a single picture.
            [self.photo1View setImage:[self.capturedImages objectAtIndex:0]];
        }
        // To be ready to start again, clear the captured images array.
        [self.capturedImages removeAllObjects];
    }

    self.imagePickerController = nil;
}




//Take photo2 and display in bottom image view
- (void)takePhoto2;
{
    [self dismissViewControllerAnimated:YES completion:NULL];

    if ([self.capturedImages count] > 0)
    {
        if ([self.capturedImages count] == 1)
        {
            // Camera took a single picture.
            [self.photo2View setImage:[self.capturedImages objectAtIndex:1]];
        }
        // To be ready to start again, clear the captured images array.
        [self.capturedImages removeAllObjects];
    }

    self.imagePickerController = nil;
}

有人知道怎么修这个东西吗?

4

4 回答 4

1

检查您的故事板或 xib 以查看“ photo2View”是否真的连接到您的插座。

我怀疑“”的 UIImageViewphoto1View也连接到“ photo2View”。

ps您还应该在@implementation中更改此行:

- (void)takePhoto2;

对此:

- (void)takePhoto2

分号只能出现在 .h 文件中的方法声明之后。

于 2014-09-06T15:01:02.260 回答
1

我看到了你的问题,无法帮助我自己以我的方式解决它。如果我遇到与您相同的问题,我会以这种方式解决它:

考虑一下,按钮的标签与 imageView 标签相同。

#import "ViewController.h"

@interface ViewController ()
- (IBAction)takePhoto:(id)sender;

@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *imageViews;

@property int tag;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)takePhoto:(id)sender {
    _tag = ((UIButton *)sender).tag;
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    [self presentViewController:imagePickerController animated:YES completion:nil];


}

#pragma mark - UIImagePickerDelegate Methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
    [picker dismissViewControllerAnimated:NO completion:nil];

    [[self getImageView:_tag] setImage:image];

}

- (UIImageView *) getImageView : (int) tag{
    for (UIImageView *imageView in _imageViews) {
        if (imageView.tag == tag) {
            return imageView;
        }
    }
    return nil;
}


@end

在这里,您可以在此处将其作为工作项目进行检查

于 2014-09-06T15:55:55.680 回答
1

你的代码有效吗?我发现两个问题-

capturedImages问题 1 -根据第二次检查,这应该像一个元素一样崩溃,if但是您在设置 imageView 时正在访问数组中的第二个元素。

if ([self.capturedImages count] == 1)
        {
            // Camera took a single picture.
            [self.photo2View setImage:[self.capturedImages objectAtIndex:1]];

问题 2 - 由于您正在清除self.capturedImages,这应该在第二种方法中抛出异常。

[self.photo2View setImage:[self.capturedImages objectAtIndex:1]];
于 2014-09-06T15:01:42.507 回答
0

您是否正确地将按钮 1 连接到 takePhoto1 并将按钮 2 连接到 takePhoto2?也许按钮 2 也在做 takePhoto1?

于 2014-09-06T15:27:02.467 回答