在我的應用程序,我加載一系列圖片來我UIImageView
已在init
方法創建像的UIImageView內存泄漏
- (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]];
}
但這個代碼會導致內存泄漏.Something像
-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];
}
只啓用ARC。它爲你解決所有的內存管理。那很簡單。 – apple16
我將創建一個分支並嘗試使用ARC。非常感謝 –
我敢打賭,如果你運行分析器,它會指出你的錯誤。 –