Цикл добавления UIImageView Какао Touch - PullRequest
3 голосов
/ 22 октября 2011

возникли проблемы при добавлении нескольких изображений в мой вид. У меня есть массив изображений и что циклически просматривать их и добавить к моему мнению. Этот фрагмент добавляет изображение для просмотра, но без рамки!

- (void)cycleImages:(NSArray *)images {
    //create an image view

   int fromLeft = 5;
   int profilesCount = [images count] - 1;

    for(int n = 0; n <= profilesCount; n++){



       //add image view to view
       [self.view addSubview:[[[UIImageView alloc] initWithFrame:CGRectMake(5, fromLeft, 48, 48)] initWithImage:[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[images objectAtIndex:n]]]]]];

        fromLeft = fromLeft + 53;

        NSLog(@"%d", fromLeft);
    }
}

Спасибо за вашу помощь!

Dex

1 Ответ

1 голос
/ 17 ноября 2011

Трудно прочитать такой код, и у вас есть утечки памяти. Пожалуйста, попробуйте это.

- (void)cycleImages:(NSArray *)images 
{
   int fromLeft = 5;
   NSURL* url = nil;
   UIImage* img = nil;
   UIImageView* imgView = nil;
   NSData* imgData = nil;

   for (NSString* imgURLStr in images)//Using enumeration much more easy and in ObjC style
   {
       //Put breakpoint here and check all varibles initialized coorecly step by step
       url = [NSURL URLWithString:imgURLStr];
       imgData = [NSData dataWithContentsOfURL:url];
       img = [[UIImage alloc] initWithData:imgData];
       imgView = [[UIImageView alloc] initWithImage:img];

       imgView.frame = CGRectMake(5, fromLeft, 48, 48);

       [self.view addSubView:imgView];

       fromLeft = fromLeft + 53;
       NSLog(@"%d", fromLeft);

       //Clean up
       [img release];
       [imgView release];
   }
}

Спасибо! * * 1004

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...