當我連續三次在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在第三次印刷後不久墜毀。
你有沒有嘗試將它設置爲零,在加載新圖像之前..我的意思imageView.image =零; – vishy 2012-04-26 12:34:51
是的,它沒有區別。它仍然崩潰:( – 2012-04-26 12:41:19
)你在日誌中看到什麼時候崩潰了?發生了什麼類型的崩潰? – DBD 2012-04-26 12:52:45