首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在ios中挑选多个镜像

如何在ios中挑选多个镜像
EN

Stack Overflow用户
提问于 2014-02-13 19:35:27
回答 2查看 247关注 0票数 0

我已经有一个用户选择四个图像在同一时间的工作代码。但我不能一次选择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]'。我找不到解决这个问题的办法。我应该如何修复我的代码,以使其按预期工作?

到目前为止,我的代码如下:

代码语言:javascript
运行
复制
- (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)];

}
EN

回答 2

Stack Overflow用户

发布于 2014-02-13 19:39:54

此错误告诉您正在选择的图像索引超出数组范围。

例如

您的数组的项目位于索引0,1,2,3,并且您正在从索引4和5中选择项目

票数 0
EN

Stack Overflow用户

发布于 2014-02-13 19:56:28

你的代码会告诉你崩溃的情况。

代码语言:javascript
运行
复制
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循环中,如下所示

代码语言:javascript
运行
复制
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;

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21753064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档