2015-09-17 44 views
0

我有一個包含字典的數組。每個字典包含UIImage &字符串值。NSMutableArray爲圖像創建問題

但是當我嘗試使用

[arr removeObjectAtIndex:button.tag];

刪除對象,然後它只是降低了計數,但圖像(對象)不會被刪除。 因此,我的圖片在嘗試刪除時重疊。

它還創建問題,當我試圖在數組中

[arr insertObject:dict atIndex:0];

這麼加的對象,我用 [_postObj.arr_images addObject:dict];代替insertObject

幫我解決這個

+2

你是什麼意思圖像重疊時試圖刪除? –

+0

你使用NSMutableArray嗎? – Misha

+0

@ShamasS,圖片數據持久存在圖片視圖 – user2526811

回答

2

對象使用引用計數和NSMutableArrayUIImageView保留UIImage對象。

這意味着,從數組刪除它不會自動從UIImageView刪除它,你必須從兩個明確的刪除:

[arr removeObjectAtIndex:button.tag]; 
_imageView.image = nil; 
0

問題是在UIImageView你的框架設置。請嘗試使用下面的代碼(未由我自己測試),看看這是否解決了你的問題。基本上,我根據以前圖像的寬度調整圖像的X位置。

CGFloat imageXM = 5.0; 
CGFloat imageXPos = 0; 

for (UIImage *image in arr_images) { 
    CGRect imageFrame = CGRectMake(imageXPos + imageXM, 0.0, image.size.width, image.size.height); 
    imageXPos = imageXPos + image.size.width; 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame]; 

    imageView.contentMode = UIViewContentModeScaleAspectFit; 
    imageView.image = image; 
    [scl addSubview:imageView]; 
} 
0

正如在代碼

[picker dismissViewControllerAnimated:YES completion:^{ 
     [arr_images insertObject:chosenImage atIndex:0]; 
     [self scrollImages_Setup2]; 

    }]; 

[arr_images insertObject:chosenImage atIndex:0];這意味着您只需要在滾動視圖中顯示一個圖像。那麼爲什麼你需要一個循環:

-(void)scrollImages_Setup2 
{  
    for (int k=0; k<arr_images.count; k++) { 

     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((CGRectGetWidth(scl.frame) * k) + CGRectGetWidth(scl.frame), 0, CGRectGetWidth(scl.frame), CGRectGetHeight(scl.frame))]; 

     imageView.contentMode = UIViewContentModeScaleAspectFit; 
     imageView.image=[arr_images objectAtIndex:k] ; 
     [scl addSubview:imageView]; 
    } 

    scl.contentSize = CGSizeMake((CGRectGetWidth(scl.frame) * arr_images.count)+CGRectGetWidth(scl.frame), CGRectGetHeight(scl.frame)); 

} 

你恰到好處的代碼只是爲了顯示圖像。您不需要滾動視圖以僅顯示一個圖像。我想你不清楚你想要什麼。

您的問題是由於循環而覆蓋圖像。所以在顯示單個圖像時,請刪除循環。請刪除scrollview的contentSize,因爲image_array會增加,當你在數組中添加多個圖像時,size的寬度將會很大。所以刪除arr_images.count。

+0

感謝您的回覆。 plz在發佈答案之前瞭解代碼。 '[arr_images insertObject:chosenImage atIndex:0];'並不意味着數組只有一個值 – user2526811

+0

感謝您的建議,但是在代碼中您總是替換索引值爲0的值,這意味着您要替換第一個圖像數組,而且你在任何地方提到你有圖像列表,這就是我設想的原因。下次我會更深入地假設。謝謝 –