0

在我的应用程序中,我正在向我的应用程序加载一系列图像,这些图像UIImageView是在initMethod上创建的

- (UIImage *)getImage:(int)currentIndex {
    NSString *path = [self getImagePath:currentIndex];
    [self.imageView setAccessibilityLabel:path]; 

    path=[bundle pathForResource:path ofType:extension];    
    return  [UIImage imageWithContentsOfFile:path] ;

}

- (void)changeImage:(int)currentIndex {

    [self.imageView setImage:nil ];
    [self.imageView setImage:[self getImage:currentIndex]];

}

但是这段代码会导致内存泄漏。比如

-malloc - 7KB - imageIO - png_malloc

我已经尝试使用适当的发布调用“[[UIImage alloc] initWithContentsOfFile]”,但仍然没有运气。

我在这里缺少什么。

大约全班

- (id)initWithFrame:(CGRect)frame andBundle:(NSBundle *) bundleTobeInjected {

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
    self.opaque = YES;
        bundle=bundleTobeInjected;
        commonFunctions=[[Common alloc] init];
        self.imageView = [[UIImageView alloc] initWithFrame:frame] ;
        [self.imageView setImage:nil ];
        self.imageView.contentMode = self.contentMode;
        [self addSubview:imageView];
        [self setAccessibilityEnabled];
    }
    return self;
}
-(void)setAccessibilityEnabled
{
    self.imageView.isAccessibilityElement=true;
}

#pragma mark - Custom Logic
- (NSString *)getImagePath:(int)currentIndex {

    NSString *path = [NSString stringWithFormat:@"%@%d%@", prefix,currentIndex,[commonFunctions imageNamedSmartForRotationSuffix]];
    NSLog(@"%@", path);
    return path;
}

- (UIImage *)getImage:(int)currentIndex {
    NSString *path = [self getImagePath:currentIndex];
    [self.imageView setAccessibilityLabel:path]; 

    path=[bundle pathForResource:path ofType:extension];    
    return  [UIImage imageWithContentsOfFile:path] ;

}

-(void)changeImage:(int)currentIndex {


    [self.imageView setImage:nil ];
    [self.imageView setImage:[self getImage:currentIndex]];


}


-(void)dealloc{
    [extension release];
    [prefix release];
    [defaultImage release];
    [image release];
    [imageView release];
    [commonFunctions release];
    [super dealloc];
}
4

1 回答 1

1

该代码不会泄漏。您的泄漏在其他地方:)

泄漏在init方法中。看看这一行:

self.imageView = [[UIImageView alloc] initWithFrame:frame];

该行创建了一个您拥有的图像视图,因为您使用了一种init方法。然后,您将其分配给再次拥有它的财产,因为我猜您的财产是retain. 将 imageView 分配给您的属性后,您需要释放它,如下所示:

self.imageView = [[[UIImageView alloc] initWithFrame:frame] autorelease];

PS你不需要这条线

[self.imageView setImage:nil ];
于 2012-09-03T14:56:02.633 回答