我已经有一个用户选择四个图像在同一时间的工作代码。但我不能一次选择2或3张图片。我想一次选择2或3张图片。
如果我选择2张图片,我会得到如下异常:Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]
如果我选择3张图片,我会得到如下异常:Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
如果我点击一张图片,我会得到这样的异常:Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'。我找不到解决这个问题的办法。我应该如何修复我的代码,以使其按预期工作?
到目前为止,我的代码如下:
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
for (UIView *v in [_scrollView subviews])
{
[v removeFromSuperview];
}
CGRect workingFrame = _scrollView.frame;
workingFrame.origin.x = 0;
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
for (NSDictionary *dict in info)
{
UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
self.chosenImages = images;
}
UIImageView *image1=[[UIImageView alloc]initWithFrame:CGRectMake(10, 240, 40, 40)];
image1.image=[images objectAtIndex:0];
[self.view addSubview:image1];
UIImageView *image2=[[UIImageView alloc]initWithFrame:CGRectMake(60, 240, 40, 40)];
image2.image=[images objectAtIndex:1];
[self.view addSubview:image2];
//self.chosenImages = images;
UIImageView *image3=[[UIImageView alloc]initWithFrame:CGRectMake(120, 240, 40, 40)];
image3.image=[images objectAtIndex:2];
[self.view addSubview:image3];
UIImageView *image4=[[UIImageView alloc]initWithFrame:CGRectMake(180, 240, 40, 40)];
image4.image=[images objectAtIndex:3];
[self.view addSubview:image4];
[_scrollView setPagingEnabled:YES];
[_scrollView setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
}发布于 2014-02-13 19:56:28
你的代码会告诉你崩溃的情况。
UIImageView *image1=[[UIImageView alloc]initWithFrame:CGRectMake(10, 240, 40, 40)];
image1.image=[images objectAtIndex:0];
[self.view addSubview:image1];
UIImageView *image2=[[UIImageView alloc]initWithFrame:CGRectMake(60, 240, 40, 40)];
image2.image=[images objectAtIndex:1];
[self.view addSubview:image2];
//self.chosenImages = images;
UIImageView *image3=[[UIImageView alloc]initWithFrame:CGRectMake(120, 240, 40, 40)];
image3.image=[images objectAtIndex:2];
[self.view addSubview:image3];
UIImageView *image4=[[UIImageView alloc]initWithFrame:CGRectMake(180, 240, 40, 40)];
image4.image=[images objectAtIndex:3];
[self.view addSubview:image4];如果我选择2张图片,我会得到这样的异常:由于未捕获的异常'NSRangeException',终止应用程序,原因:'* -__NSArrayM objectAtIndex::索引3超出边界0 ..2
在这种情况下,你不能做[images objectAtIndex:3]。因为该数组只包含2个图像。其他情况也是如此。
根据评论进行更新:
只需尝试将[self.view addSubview:imageview ];添加到您的for循环中,如下所示
for (NSDictionary *dict in info)
{
UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[self.view addSubview:imageview ];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
self.chosenImages = images;
}https://stackoverflow.com/questions/21753064
复制相似问题