【问题标题】:how to pick the multiple images in ios如何在ios中选择多个图像
【发布时间】:2014-03-12 06:17:14
【问题描述】:

我正在为 iPhone 制作一个应用程序,并希望让用户能够从他们的照片库中多选图像。我已经有一个工作代码供用户一次选择四个图像。但我不能一次选择 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)];

}

【问题讨论】:

  • 你必须回复那些给你答案的用户。

标签: ios iphone objective-c uiimageview uiimagepickercontroller


【解决方案1】:

这个错误告诉你你选择的图像索引超出了数组。

例如

您的数组在索引 [0,1,2,3] 处具有项目,并且您正在从索引中选择项目,例如 4 和 5

【讨论】:

  • 感谢您的回复。请告诉我任何解决方案?我的代码中有任何更改。请告诉我......提前谢谢......
【解决方案2】:

您的代码说明了您的崩溃。

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 张图片,我会收到如下异常:Terminating app due to 未捕获的异常 'NSRangeException',原因:'* -[__NSArrayM objectAtIndex:]: index 3 越界[0 .. 2]

在这种情况下你不能做[images objectAtIndex:3]。因为该数组仅包含 2 张图像。其他情况也一样。

根据评论更新:

只需尝试在您的 for 循环中添加 [self.view addSubview:imageview ];,如下所示

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;

}

【讨论】:

  • 如果您总是选择 4 张图片,那么您的代码可以工作,但在其他情况下,您需要根据您选择的图片数量创建图像视图。
  • 感谢您的回复。如何根据您的建议修改我的代码。请告诉我朋友..提前谢谢.....
  • 根据您的评论更新了答案。请检查它是否有帮助。
  • 感谢您的回复。但仍然得到相同的异常...告诉我任何解决方案
  • 感谢您的回复。我没有删除任何代码.....但是无法正常工作请给我任何想法...或示例代码。感谢您的回复....
猜你喜欢
  • 1970-01-01
  • 2023-03-09
  • 2016-09-07
  • 1970-01-01
  • 1970-01-01
  • 2014-08-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-11
相关资源
最近更新 更多