2014-09-06 120 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