0

私のアプリでは、次のようなメソッドUIImageViewで作成された一連の画像をロードしていますinit

- (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

適切なリリース呼び出しで「[[UIImagealloc]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 に答える