1

我有 200 多张图像,我希望通过循环将它们添加到 NSArray,我该如何打开它:

NSArray *imgArray = [NSArray arrayWithObjects:
                     [UIImage imageNamed:@"Ski_Seq0000.jpg"],
                     [UIImage imageNamed:@"Ski_Seq0001.jpg"],
                     [UIImage imageNamed:@"Ski_Seq0002.jpg"],
                     [UIImage imageNamed:@"Ski_Seq0003.jpg"],
                     [UIImage imageNamed:@"Ski_Seq0004.jpg"],
                     [UIImage imageNamed:@"Ski_Seq0005.jpg"],
                     [UIImage imageNamed:@"Ski_Seq0006.jpg"],
                     [UIImage imageNamed:@"Ski_Seq0007.jpg"],nil];

循环?

4

6 回答 6

5
 NSMutableArray* imgArray = [[NSMutableArray alloc] arrayWithCapacity:200];

 for(int i = 0; i <= 200; i++)
 {
     UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:@"Ski_Seq%04d.jpg",i]];
     [imgArray addObject:image];
 } 

但是您应该注意内存要求,具体取决于图像的大小。

于 2012-09-20T06:44:55.370 回答
3

像这样使用NSMutableArray

NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (int x = 0; x < 200; x++) {
    [tempArray addObject:[NSString stringWithFormat:@"Ski_Seq%04d.jpg",x]];
}

NSArray *imgArray = [NSArray arrayWithArray:tempArray];
[tempArray release];//If you are not using ARC.
于 2012-09-20T06:43:08.130 回答
2

做这个:

NSMutableArray *ArrImgs = [[NSMutableArray alloc] init];
for(int i=0; i<=199;i++)
{
  NSString *strImg = [NSString stringithFormat:@"Ski_Seq%04d.jpg",i];
  [ArrImgs  addObject:[UIImage imageNamed:strImg]];

}
于 2012-09-20T06:43:19.520 回答
2

你真的有必要将图像保存在数组中吗?仅将图像的名称存储在数组中怎么样?

NSMutableArray* imagesName = [[NSMutableArray alloc] arrayWithCapacity:200];
for(int i = 0; i <= 200; i++){
    [imagesName addObject:[NSString stringWithFormat:@"Ski_Seq%04d",i]];
}

获取图像:

for(int i = 0; i <= [imagesName count]; i++){
   UIImage *image = [UIImage imageNamed:[imagesName objectAtIndex:i]];
}

如果您确实需要将图像存储到数组中,可以在此处参考其他答案:)

于 2012-09-20T07:00:41.503 回答
1
 NSMutableArray *te=[[NSMutableArray alloc]init];

    for (int i=0; i<200; i++)
    {

        [te addObject: [UIImage imageNamed: [NSString stringWithFormat:@"Ski_Seq%04d.jpg",i]]];
    }

    NSArray *imgArray=[[NSArray alloc]initWithArray:[ te mutableCopy]];

    NSLog(@"the array::%@",imgArray);

尝试这个...

于 2012-09-20T06:52:04.237 回答
-1
 you try this code:

    @interface AnimationViewController : UIViewController
     {

         NSMutableArray *arrayImages;

          UIImageView *infoImage;

          NSTimer *timer;

            int i;

           NSTimeInterval timeInterval;

           NSDate *start;

       }



  -(void)animationStart
   {

   if (i==200) {

  [timer invalidate];
   timer=nil;        
   return;

   }

   NSLog(@"%d",i);

 infoImage.image=[UIImage imageNamed:[arrayImages objectAtIndex:i]];

  i++;
  }


      -(void)viewWillAppear:(BOOL)animated
       {
          [super viewWillAppear:animated];


        NSDate *start1 = [NSDate date];

       timeInterval = [start1 timeIntervalSince1970];

  timer=[NSTimer scheduledTimerWithTimeInterval:0.06 target:self selector:@selector(animationStart) userInfo:nil repeats:YES];

     NSLog(@"time---");

       }


    - (void)viewDidLoad
    {
     [super viewDidLoad];

    infoImage=[[UIImageView alloc]init];
    infoImage.frame=CGRectMake(0, 0, 320, 460);
    infoImage.backgroundColor=[UIColor clearColor];
    infoImage.image=[UIImage imageNamed:@"guideAmbi.png"];
    [self.view addSubview:infoImage];

    i=0;

   infoImage=[[UIImageView alloc]init];
   infoImage.frame=CGRectMake(0, 0, 320, 460);
   infoImage.backgroundColor=[UIColor clearColor];
   infoImage.image=[UIImage imageNamed:@"Info1.png"];
    [self.view addSubview:infoImage];

   arrayImages=[[NSMutableArray alloc]initWithCapacity:200];

    for (int is=1; is<=300; is++) 
    {
    NSString *str=[NSString stringWithFormat:@"Info%i.png",is];
    [arrayImages addObject:str];

     }  
于 2012-09-20T07:14:38.073 回答