1

我有图像数组,其中包含第一张图像中的两个图像,它在图像视图上正确显示,因为我在视图中编写代码是否加载为`

NSString *urlString=[NSString stringWithFormat:@"%@",[imageArray1 objectAtIndex:i]];
  NSLog(@"selected image:%@",urlString);
NSURL *url =[NSURL URLWithString:urlString];
NSData *imagedata =[[NSData alloc]initWithContentsOfURL:url];
UIImage *image=[[UIImage alloc]initWithData:imagedata];
[imgvw setImage:image];

现在我需要在单击条形按钮时显示下一个图像,该代码是`

i=i+1;
NSLog(@"i=%d",i);
NSString *urlstr=[NSString stringWithFormat:@"%@",[imageArray1 objectAtIndex:i]];
NSLog(@"selected image:%@",urlstr);
NSURL *url =[NSURL URLWithString:urlstr];
NSData *imagedata =[[NSData alloc]initWithContentsOfURL:url];
UIImage *image1=[[UIImage alloc]initWithData:imagedata];
[imgvw setImage:image1];

但是在线崩溃 `NSString *urlstr=[NSString stringWithFormat:@"%@",[imageArray1 objectAtIndex:i]]; 我不明白为什么会发生请提出一些建议?

4

2 回答 2

0
int i=0;

NSString *urlString=[NSString stringWithFormat:@"%@",[imageArray1 objectAtIndex:i]];
SURL *url =[NSURL URLWithString:urlString];
NSData *imagedata =[[NSData alloc]initWithContentsOfURL:url];
UIImage *image=[[UIImage alloc]initWithData:imagedata];
[imgvw setImage:image];


in button action event.

if(i<[imageArray1 count]-1)
{
i++;

NSString *urlString=[NSString stringWithFormat:@"%@",[imageArray1 objectAtIndex:i]];
SURL *url =[NSURL URLWithString:urlString];
NSData *imagedata =[[NSData alloc]initWithContentsOfURL:url];
UIImage *image=[[UIImage alloc]initWithData:imagedata];
[imgvw setImage:image];


}
else
{
// 
/NSLog(@" you reach the last items ");

}
于 2012-06-16T09:35:33.713 回答
0

int i;在 yourviewcontroller.h 文件中声明

我 = 0; 在 viewDidLoad方法中

您在按下按钮时实现代码。

 int noOfImages = [imageArray1 count];

    if(++i< noofImages){

    NSString *urlString=[NSString stringWithFormat:@"%@",[imageArray1 objectAtIndex:i]];
    SURL *url =[NSURL URLWithString:urlString];
    NSData *imagedata =[[NSData alloc]initWithContentsOfURL:url];
    UIImage *image=[[UIImage alloc]initWithData:imagedata];
    [imgvw setImage:image];

    }
    else
    {
    i =0;
    NSString *urlString=[NSString stringWithFormat:@"%@",[imageArray1 objectAtIndex:i]];
    SURL *url =[NSURL URLWithString:urlString];
    NSData *imagedata =[[NSData alloc]initWithContentsOfURL:url];
    UIImage *image=[[UIImage alloc]initWithData:imagedata];
    [imgvw setImage:image];
    }
于 2012-06-16T09:42:09.483 回答