当UIImageView.image设置2-3次时应用程序崩溃



当我连续三次在 UIImageView 上设置图像属性时,我的应用程序崩溃(有时两次就足够了)。有几次我在应用程序关闭之前看到内存警告,但大多数时候它只是崩溃。该应用程序不会在模拟器中崩溃,因此我很确定这是内存问题。

这是我在设置 image 属性时使用的代码:

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

UIImages使用[UIImage imageWithData:]方法分配:

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

前两次图像设置正确,但第三次崩溃。它与特定的图像文件无关。我尝试了 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

这是初始化实现:

-(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"对象列表,该对象通过此调用添加到循环中:

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

我有 9 张名为 00001.jpg、00002.jpg、.....、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];    
}

日志打印"设置图像:0000X:"三次,但在第三次打印后不久崩溃。

不要将 UIImage 对象存储在 MyObject 类中,而是尝试仅存储文件的文件名。

@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];
 }

至少你应该在添加到映像数组后释放这些 MyImage 实例。

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

此代码应为 -

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

或者使用自动释放。

您的代码应该会出现内存泄漏。

尝试使用以下命令从捆绑包中获取图像

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

当您收到内存不足警告时,是否有可能释放某个属性,然后尝试使用它,但您正在使用 nil?

您是否尝试过在模拟器中模拟内存不足警告以查看它是否崩溃?硬件>模拟内存警告

最新更新