如何在ios中拾取多个图像
我正在为iPhone制作一个应用程序,希望让用户能够从照片库中多选择图像。我已经有一个工作代码供用户一次选择四个图像。但我不能一次选择2或3张图像。我想一次选择2或3个图像 如果我选择了2个图像,则会出现如下异常:由于未捕获的异常“NSRangeException”而终止应用程序,原因:“***-[\uu NSArrayM objectAtIndex:]:索引3超出边界[0..2] 如果我选择了3个图像,我会得到如下异常:由于未捕获的异常“NSRangeException”而终止应用程序,原因:“***-[\uu NSArrayM objectAtIndex:]:索引2超出边界[0..1]” 如果我单击一个图像,会出现如下异常:由于未捕获的异常“NSRangeException”而终止应用程序,原因:“***-[\uu NSArrayM objectAtIndex:]:索引1超出边界[0..0]”。我找不到解决这个问题的办法。我应该如何修复代码以使其按预期工作 以下是我目前的代码:如何在ios中拾取多个图像,ios,iphone,objective-c,uiimageview,uiimagepickercontroller,Ios,Iphone,Objective C,Uiimageview,Uiimagepickercontroller,我正在为iPhone制作一个应用程序,希望让用户能够从照片库中多选择图像。我已经有一个工作代码供用户一次选择四个图像。但我不能一次选择2或3张图像。我想一次选择2或3个图像 如果我选择了2个图像,则会出现如下异常:由于未捕获的异常“NSRangeException”而终止应用程序,原因:“***-[\uu NSArrayM objectAtIndex:]:索引3超出边界[0..2] 如果我选择了3个图像,我会得到如下异常:由于未捕获的异常“NSRangeException”而终止应用程序,原因:
- (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)];
}
此错误告诉您正在选择的图像索引超出数组范围 例如
您的数组在索引[0,1,2,3]处有项,并且您正在从索引(如4和5)中选择项。您的代码说明了您的崩溃
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”,原因:'*-[\uu 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;
}
感谢您的回复。请告诉我解决方案?我的代码中有任何更改。请告诉我……提前感谢……如果您总是选择4个图像,那么您的代码将正常工作,但在其他情况下,您需要根据所选图像的数量创建ImageView。感谢您的回复。如何根据您的建议修改我的代码。请告诉我朋友..提前感谢..根据您的评论更新了答案。请检查它是否有用。感谢您的回复。但仍然会遇到相同的异常情况…告诉我任何解决方案感谢您的回复。我没有删除任何代码…但不工作请给我任何想法…或示例代码。感谢您的回复…您必须回复给您问题答案的用户。