2014-09-06 116 views
1

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

我还没有实现任何代码来保存照片,所以我并不担心图片视图中持续存在的照片。

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

//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; 
} 

任何人都知道如何解决这个问题?

回答

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 

在这里,您可以check this as working project here

+1

谢谢,完美的工作!该示例项目非常有用。干杯! – narner 2014-09-06 23:18:39

1

检查你的故事板或XIB,看看是否“photo2View”实在是连接到电源插座。

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

p.s.你也应该改变这条线在你的@implementation:

- (void)takePhoto2; 

这样:

- (void)takePhoto2 

分号应只来在你的.h文件中的方法声明之后。

+0

嘿@迈克尔, 是的,它看起来像“photo2View”连接到我的插座。我只是检查; “photo1View”的UIImageView未连接到“photo2View”。 谢谢你的分号纠正!真的很感激它。 – narner 2014-09-06 15:11:02

1

你的代码是否工作?我发现两个问题 -

问题1 - 这应该崩溃,因为capturedImages有一个元素根据第二个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]]; 
+0

嗨Vikram,是的,我的代码似乎工作(没有崩溃或抛出异常)。我确实收到警告(如下所示): 警告:尝试从视图控制器中解除演示或解散过程! – narner 2014-09-06 15:24:14

+0

那么如何[self.photo2View setImage:[self.capturedImages objectAtIndex:0]]; – auspicious99 2014-09-06 15:29:08

+0

你好@吉祥99,我只是试过了;似乎没有注意到 [self.photo2View setImage:[self.capturedImages objectAtIndex:1]]; and [self.photo2View setImage:[self.capturedImages objectAtIndex:0]]; – narner 2014-09-06 15:37:17

0

您是否正确连接按钮1以拍摄照片1和按钮2拍摄照片2?也许按钮2也在做takePhoto1?

+0

是的,只是检查;每个按钮都连接到相应的方法:/ – narner 2014-09-06 15:33:00

+0

嗯,另一个可以尝试的方法是缩小它的范围,就是在两种方法中都使用NSLog语句,以确保在每种情况下都调用正确的方法 – auspicious99 2014-09-06 15:43:30

+0

另一种可能性是,也许在别的地方你不小心将self.photo2View设置为self.photo1View? – auspicious99 2014-09-06 15:44:24