1

私のテスト アプリには、2 つのボタン (takePhoto1 と takePhoto2) と 2 つの UIImageView (photo1View と photo2View) があります。最初のボタンは写真を撮り、それを最初の画像ビューに表示します。2 番目のボタンは別の写真を撮り、それを 2 番目の画像ビューに表示することになっています。しかし、なぜか2枚目の写真がphotoView2ではなくphotoView1に表示されてしまいます。

写真を保存するためのコードはまだ実装していないので、画像ビューに写真が残っていることを心配する必要はありません。

私が理解する必要があるのは、2 番目の写真が撮影されたときに適切なビューで表示されるようにする方法です。これが私がこれまでに持っているものです:

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

あなたのコードは機能しますか?私は2つの問題を見つけました -

問題 1 - これはcapturedImages、2 番目のチェックに従って 1 つの要素があるためクラッシュするはずifですが、imageView の設定中に配列内の 2 番目の要素にアクセスしています。

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

問題 2 - をクリアしているためself.capturedImages、2 番目のメソッドで例外がスローされるはずです。

[self.photo2View setImage:[self.capturedImages objectAtIndex:1]];
于 2014-09-06T15:01:42.507 に答える
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 に答える