如何通过单击按钮从图像阵列显示图像视图上的下一个图像

时间:2012-06-16 09:26:53

标签: objective-c ipad ios5 ios4 xcode4.2

我有图像阵列,其第一张图像中有两张图像,它在图像上正确显示视图,因为我在视图中编写了代码“加载为”

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]]; 我不明白为什么会这样,请提出建议?

2 个答案:

答案 0 :(得分: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 ");

}

答案 1 :(得分:0)

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

i = 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];
    }