在我的测试应用程序中,我有两个按钮(takePhoto1和takePhoto2)和两个UIImageViews(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;
}
任何人都知道如何解决这个问题?
答案 0 :(得分:1)
检查故事板或xib以查看" photo2View
"真的与你的插座有关。
我怀疑UIImageView for" photo1View
"也连接到" photo2View
"。
P.S。您还应该在@implementation中更改此行:
- (void)takePhoto2;
到此:
- (void)takePhoto2
分号只应在.h文件中的方法声明之后出现。
答案 1 :(得分: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]];
答案 2 :(得分: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
答案 3 :(得分:0)
您是否正确连接按钮1以拍摄照片1和按钮2拍摄照片2?也许按钮2也在做takePhoto1?