2010-10-12 134 views
0

嗨 我是iphone新手..我已經將圖像存儲在數組中。我想知道如何在UIImageView中顯示這些圖像,其中UIImageView應根據文件列表計數動態創建增加,for循環。這是代碼在for循環中動態創建uiimageview

NSArray *filelist; 
NSFileManager *filemgr; 
int count; 
int i=0; 
int t=0; 
filemgr = [NSFileManager defaultManager]; 
filelist = [filemgr directoryContentsAtPath: @"/Mypath/"]; 
NSMutableArray *imageVwArr = [[NSMutableArray alloc]init]; 
count = [filelist count]; 
count++; 
for(i = 0; i < count; i++)         
{ 
    UIImageView *imgVw = [[UIImageView alloc] initWithImage:[filelist objectAtIndex:i]]; 
    [imgVw setUserInteractionEnabled:YES]; 
    [imgVw setTag:i]; 
    imgVw.frame=CGRectMake(t, 0, 480, 320); 
    [imageVwArr addObject:imgVw]; 
    [self.view addSubview:imgVw]; 
    [imgVw release]; 
    t=t+480;   
} 

它顯示了錯誤:「SIGABRT」: 投擲的「NSException」 程序實例接收信號後終止調用。

在那裏我做錯了,我有沒有關於提前創建動態的UIImageView 感謝想法....

+0

這是怎麼回事? – zem 2010-10-12 12:25:36

回答

1
[filemgr directoryContentsAtPath: @"/Mypath/"]; 

這很可能會回到零。因爲這條路不存在。您無法訪問iphone上的完整文件系統。

將您的文件放入應用程序Document目錄中。

NSString *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 

編輯:我只是嘗試這樣做,並沒有引發異常,所以你的錯誤是在其他地方。
編輯2:你正在增加計數,爲什麼?通過遞增計數,for循環嘗試訪問不在數組中的對象。

如果你的fileArray有10個條目數將是10.然後你增加計數,所以計數將是11,但你的數組仍然只有10個條目。
您的for循環將從索引0(這是您的第一個文件名)開始,直到它訪問索引9(這是您的第10個文件名)一切正常。

在下一個循環中,它嘗試訪問索引10處的文件名。此索引超出範圍。

EDIT3:

下一個錯誤:initWithImage:想要一個UIImage的,而不是一個文件名。用途:

NSString *fullPath = [yourImagePath stringByAppendingPathComponent:[filelist objectAtIndex:i]]; 
UIImage *image = [[[UIImage alloc] initWithContentsOfFile:fullPath] autorelease] 
UIImageView *imgVw = [[UIImageView alloc] initWithImage:image]; 

EDIT4: 也許你想創建imageViews只有一個的UIImage可以從您的文件被創建。所以,如果碰巧有一個文件,這是不是你不創建一個空的ImageView有效的圖像:

if (!image) 
    continue; 
UIImageView *imgVw = [[UIImageView alloc] initWithImage:image]; 

EDIT5 & LAST: 我實現一個imageViewController的。請不要直接使用它,嘗試理解它並學習一些東西。它離完美還很遙遠。

+0

k謝謝了很多使用for循環我如何創建多個imageviews當我滾動imageview我想查看所有圖像我如何得到...先前感謝 – Rajkumar 2010-10-12 12:56:54

+0

有一個文件「.DS_Store」他們不會出現,但arraylist包含這個文件,所以只有我增加計數 – Rajkumar 2010-10-12 13:06:52

+0

你的forloop做到這一點,它創建了多個imageviews。將它們添加到uview,並在uiscrollview中顯示此uiview。 – 2010-10-12 13:08:22