简体   繁体   English

如何在iOS中选择多个图像

[英]how to pick the multiple images in ios

I am making an app for iPhone and want to give users the ability to multiselect images from their photo-library. 我正在为iPhone开发一个应用程序,希望为用户提供从其照片库中多选图像的功能。 I already have a working code for user to select four images at a time. 我已经有了一个工作代码,供用户一次选择四个图像。 But I can't select 2 or 3 images at a time. 但是我不能一次选择2或3张图像。 I want to select the 2 or 3 images at a time. 我想一次选择2或3张图像。

If I select 2 images I got exception like this: Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2] 如果选择2张图像,则会出现如下异常: Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]

If I select 3 images I get exception like this: Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]' 如果选择3张图像,则会出现如下异常: Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'

If I click one image I get this exception like this: Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]' . 如果单击一个图像Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'如下异常: Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]' I can't find a solution to this problem. 我找不到解决此问题的方法。 How should I fix my code to make this work as expected? 我应该如何修复我的代码以使其按预期工作?

Here's my code so far: 到目前为止,这是我的代码:

- (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)];

}

This error is telling you the image index which you are selecting is beyond of array. 此错误告诉您所选择的图像索引超出数组范围。

for eg 例如

your array having item at index [0,1,2,3] and you are selecting item from index like 4 and 5 您的数组在索引[0,1,2,3]处有项目,并且您正在从索引中选择项目4和5

Your code tells about your crash. 您的代码说明了崩溃。

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];

If I select 2 images I got exception like this: Terminating app due to uncaught exception 'NSRangeException', reason: ' * -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2] 如果选择2张图像,则会出现如下异常:由于未捕获的异常'NSRangeException'而终止应用程序,原因:' * -[__ NSArrayM objectAtIndex:]:索引3超出范围[0 .. 2]

In this case you cant do [images objectAtIndex:3] . 在这种情况下,您不能执行[images objectAtIndex:3] Because the array contains only 2 images. 因为数组仅包含2个图像。 Likewise the other cases also. 同样,其他情况也是如此。

UPDATE based on comment: 根据评论更新:

Just try adding [self.view addSubview:imageview ]; 只需尝试添加[self.view addSubview:imageview ]; in your for loop like given below 在您的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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM