2011-08-18 2 views
0

Я пытаюсь отобразить изображения из массива в uiimageview, но отображает только последнее изображение. Есть рекомендации?Показать изображения из массива в UIImageView

for (int i = 0; i < [fileArr count]; i++) 
{ 
       NSArray *fileNameArr = [[fileArr objectAtIndex:i] componentsSeparatedByString:@"."]; 
check=line; 
       NSString *msg = [textView.text stringByAppendingString:[NSString stringWithFormat:@"Opening image: %@\n",[fileArr objectAtIndex:i]]]; 

       [textView performSelectorOnMainThread:@selector(setText:) withObject:msg waitUntilDone:NO]; 

       line=line+1; 

       [image removeFromSuperview]; 
       [image release]; 
       image = nil; 
       //specify the image path, open the image file with UIImage library 
       NSString *imagePath = [NSString stringWithFormat:@"%@/%@",jfn1,[fileArr objectAtIndex:i]]; 
       NSString *imageFile = [NSHomeDirectory() stringByAppendingPathComponent:imagePath]; 
       NSString *pathExtension = [imageFile pathExtension]; 
       NSLog(@"----------------------------"); 
       NSLog(@"ImageFile: %@\n",imageFile); 
       NSLog(@"File Extention: %@\n", pathExtension); 
       NSLog(@"Interger value: %i\n",i); 

       UIImage *myImage = [UIImage imageWithContentsOfFile:imageFile]; 
       NSLog(@"Image Width: %f", myImage.size.width); 
       NSLog(@"Image height: %f", myImage.size.height); 

       image = [[UIImageView alloc] initWithImage:myImage]; 
       image.contentMode = UIViewContentModeScaleAspectFit; 
       [scrollView addSubview:image]; 
+0

Как и что вы заполняете fileArr? Если возможно, отправьте код. –

+0

вы удаляете свое изображение в каждой итерации. Зачем винить «это» за показ только последнего? –

+0

- (NSArray *) listAndPopulateFileArray: (NSString *) jfn1 { \t \t NSFileManager * fileManager = [NSFileManager defaultManager]; \t NSString * jFolder = [NSHomeDirectory() stringByAppendingPathComponent: jFolder]; \t \t NSArray * imageList = [fileManager contentsOfDirectoryAtPath: jobFolder error: nil]; \t \t return imageList; \t } – Carissa

ответ

2

Потому что вы всегда

[image removeFromSuperview]; 

перед вами

[scrollView addSubview:image]; 

в течение цикла.

Как это могло не отображаться только последнее изображение?

+0

'Cos Я хочу удалить любое изображение, показанное на uiimageview. – Carissa

+0

Не нужно вызывать 'removeFromSuperview' перед' addSubView', потому что при вызове 'addSubView' подвью автоматически удаляется из предыдущего супервизора, а затем добавляется как под просмотр другого представления. – EmptyStack

+0

Если у вас есть 10 изображений для отображения, вам нужно создать 10 имитаций для них. Вам также нужно установить разные позиции для этих изображений, самостоятельно, иначе они будут покрыты друг другом. – xuzhe

Смежные вопросы