2012-04-26 138 views
0

當我連續三次在UIImageView上設置圖像屬性時(有時兩次就足夠了),我的應用程序崩潰。有幾次我在應用程序關閉之前看到了內存警告,但大多數時候它剛剛崩潰。該應用程序不會在模擬器中崩潰,所以我很確定這是一個內存問題。設置UIImageView.image時,應用程序崩潰2-3次

這裏的設置圖像屬性,當我使用的代碼:

-(void)changeBgPictureTo:(UIImage *)img 
{ 
    [self.backgroundImage setImage:img]; 
} 

的UIImages分配與[UIImage imageWithData:]方法:

[UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:type]]]; 

圖像設置正確的第2次,但崩潰第三次。它與特定的圖像文件無關。我已經嘗試了10個不同的圖像,這沒有什麼區別。

如何讓UIImageView卸載先前加載的圖像?

編輯: 好吧,我一直在問整個代碼,所以這裏有雲:

@interface MyImage : NSObject 
{ 
    UIImage* image; 
    int imgId; 
    NSArray* colors; //contains 'UIColor' objects. 
} 

@property (nonatomic, retain) UIImage* image; 
@property (nonatomic, retain) NSArray* colors; 
@property (nonatomic, readwrite) int imgId; 

-(id)initWithFileName:(NSString*)fileName withType:(NSString*)type andId:(int)imgId andColors:(NSArray*)colorArray; 

@end 

這裏是:

我帶班,看起來像這樣的工作在init執行:

-(id)initWithFileName:(NSString*)fileName withType:(NSString*)type andId:(int)imageId andColors:(NSArray*)colorArray 
{ 
    self = [super init]; 

    if (self) 
    { 
     self.colors = [NSArray arrayWithArray:colorArray]; 
     self.imgId = imageId; 
     self.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:type]]]; 
    } 

return self; 

}

我有具有在一個循環添加此調用「MYIMAGE」對象列表的DataController類:

[self.myImages addObject:[[MyImage alloc] initWithFileName:[NSString stringWithFormat:@"0000%d", i] withType:@"jpg" andId:i andColors:colors]]; 

我有一個名爲00001.jpg,00002.jpg,9張圖片... ..,00008.jpg。

該數據控制器具有這樣的方法:

-(MyImage *)getImageWithId:(int)imgId 
{ 
    for (MyImage* img in self.myImages) 
    { 
     if (img.imgId == imgId) 
      return img; 
    } 
    return nil; 
} 

的getImageWithId方法被稱爲像這樣:

-(void)btnPushed:(id)sender 
{ 
    [self.delegate changeBgPictureTo:[self.imgDataController getImageWithId:((UIButton*)sender).tag]];  
} 

的changeBgPictureTo方法是,這使得所述圖像屬性的設置方法:

-(void)changeBgPictureTo:(MyImage *)img 
{ 
    NSLog(@"Setting image: %d", img.imgId); 
    [self.backgroundImage setImage:img.image];  
} 

日誌打印「Setting image:0000X:」三次,b在第三次印刷後不久墜毀。

+1

你有沒有嘗試將它設置爲零,在加載新圖像之前..我的意思imageView.image =零; – vishy 2012-04-26 12:34:51

+0

是的,它沒有區別。它仍然崩潰:( – 2012-04-26 12:41:19

+0

)你在日誌中看到什麼時候崩潰了?發生了什麼類型的崩潰? – DBD 2012-04-26 12:52:45

回答

2

獲得圖像代替存儲的MyObject類裏面的UIImage對象,儘量只存儲文件的文件名。

@interface MyImage : NSObject 
{ 
    NSString *fileName;//UIImage* image; 
    int imgId; 
    NSArray* colors; //contains 'UIColor' objects. 
} 

@property (nonatomic, retain) NSString *fileName;//@property (nonatomic, retain) UIImage* image; 
@property (nonatomic, retain) NSArray* colors; 
@property (nonatomic, readwrite) int imgId; 

-(id)initWithFileName:(NSString*)fileName withType:(NSString*)type andId:(int)imgId andColors:(NSArray*)colorArray; 

和實施

-(id)initWithFileName:(NSString*)fileName withType:(NSString*)type andId:(int)imageId andColors:(NSArray*)colorArray 
{ 
    self = [super init]; 

    if (self) 
    { 
     self.colors = [NSArray arrayWithArray:colorArray]; 
     self.imgId = imageId; 
     //self.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:type]]]; 
     self.fileName = [[NSBundle mainBundle] pathForResource:fileName ofType:type]]; 
     } 
     return self; 
} 

然後在功能

-(void)changeBgPictureTo:(MyImage *)img 
{ 
    NSLog(@"Setting image: %d", img.imgId); 
    //[self.backgroundImage setImage:img.image];  
    UIImage *img = [UIImage imageWithContentsOfFile:[img fileName]]; 
    [self.backgroundImage setImage:img]; 
} 
+0

這解決了我的問題,謝謝:) – 2012-04-27 08:01:40

+1

您可能還想通過@ sky1224添加修復程序來修復內存泄漏。 – nhisyam 2012-05-02 02:11:18

0

嘗試使用下面從包

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yourImageName.png" ofType:nil]]; 
+0

沒有變化仍然崩潰 – 2012-04-26 12:47:49

+0

你能告訴我圖像的大小 – vishy 2012-04-26 12:48:41

+0

在300 KB和450 KB之間沒有什麼巨大的 – 2012-04-26 12:50:59

0

是否有可能,當您收到內存不足警告你釋放一定屬性,然後嘗試使用它,但你正在與零工作?

您是否嘗試過在模擬器中模擬低內存警告來查看它是否崩潰?硬件>模擬內存警告

+0

是的,我嘗試過使用模擬內存警告。它看起來不像我的崩潰有什麼關係。 – 2012-04-27 06:16:30

2

至少你應該添加到你的圖像陣列後釋放這些MyImage實例。

[self.myImages addObject:[[MyImage alloc] initWithFileName:[NSString stringWithFormat... 

此代碼應該是 -

MyImage* img = [[MyImage alloc] initWithFileName:[NSString stringWithFormat... 
[self.myImages addObject:img]; 
[img release]; 

,或者使用自動釋放。

你的代碼應該會得到內存泄漏。

相關問題