2014-04-30 84 views
0

我必須在NSMutableArray中緩存一些UIImageView,但是當我進入這個數組時總是空的。NSArray中的UIImageView

我用這個代碼:

NSMutableArray *currentImages = [[NSMutableArray alloc] init]; 
UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(20, 8, 50, 50)]; 
for (int i = 0; i< 20; i++) { 
    imageV.image = [UIImage imageNamed:@"defaut.png"]; 
    [currentImages addObject:imageV]; 
} 

你能幫助我嗎?

+0

您需要展示更多代碼。你在哪裏存儲'currentImages'?你爲什麼要將圖像視圖存儲在數組中?這是你的真實代碼嗎? – Wain

回答

1

你可能想嘗試這樣的:

NSMutableArray *currentImages = [[NSMutableArray alloc] init]; 

for (int i = 0; i< 20; i++) { 
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(20, 8, 50, 50)]; 
    imageV.image = [UIImage imageNamed:@"defaut.png"]; 
    [currentImages addObject:imageV]; 
} 

不知道你需要這個,雖然,因爲它可能會降低你的表現。我通常將UIImage名稱作爲NSStrings存儲在NSArray中,然後按照我的意願做任何事情。

1

你總是添加相同的imageview,其中你改變了圖像,你應該在你的循環內重新創建imageview。

取決於你想達到什麼,它可能會更好地存儲圖像,而不是圖像瀏覽,並更新你的一個圖像與圖像。 緩存視圖很少是你想要的。

+0

然後,OP將有20個引用到相同的圖像視圖,這當然不是他的意圖,但與他聲稱的「空白」不同。 – Droppy

+0

我必須插入imageview,因爲我必須緩存它們。這個代碼只是一個例子 – Anna565

+0

@ Anna565你必須顯示你訪問數組的位置,因爲沒有足夠的上下文來確定你爲什麼看不到內容 – Pieter