[英]UIImage dealloc is never called
我試圖弄清楚為什么我的應用程序使用大量內存,然后在幾次內存警告后崩潰。 Instrument VM Tracker顯示它使用〜30Mb作為臟內存。 分配顯示10-15 Mb,不是很多。 一旦應用程序處理了顯示大量圖像(縮略圖)的操作,我就認為圖像就是我應該看的東西。 我不使用標准的UIImage imageNamed方法,而是使用imageWithData並進行緩存。 當系統發送內存警告時,我將清理緩存存儲。 為了確保不再需要我創建的圖像,我將UIImage子類化,並覆蓋了imageWithData,release和dealloc方法。 我可以看到調用了imageWithData,但從未調用過release和dealloc。 這就是我這樣做的方式:
BaseUIimage.h
@interface BaseUIimage : UIImage
@end
BaseUIimage.m
#import "BaseUIimage.h"
@implementation BaseUIimage
+ (id)imageWithData:(NSData *)data {
NSLog(@"UIImage imageWithData");
return [UIImage imageWithData:data];
}
- (id)retain {
NSLog(@"UIImage retain: %d", [self retainCount]);
return [super retain];
}
- (oneway void)release {
NSLog(@"UIImage release: %d", [self retainCount]);
[super release];
}
- (id)autorelease {
NSLog(@"UIImage autorelease: %d", [self retainCount]);
return [super autorelease];
}
- (void)dealloc {
NSLog(@"UIImage deallocated");
[super dealloc];
}
@end
我的緩存代碼:
。H
#import "BaseUIimage.h"
@interface UIImageCached : BaseUIimage
// CACHE
+ (NSMutableDictionary*) cache;
+ (void) cleanCache;
+ (UIImageCached *)imageNamed:(NSString *)imageName;
+ (UIImageCached *)retinaImageNamed:(NSString *)imageName;
+ (UIImageCached *)imageFromPath:(NSString *)imagePath;
.m
#import "UIImageCached.h"
@implementation UIImageCached
static NSMutableDictionary *data;
+ (NSMutableDictionary*) cache {
if (data == nil)
data = [[NSMutableDictionary alloc] initWithCapacity:150];
return data;
}
+ (void) cleanCache {
NSLog(@"Cache cleaned images: %d", data.count);
for (BaseUIimage *image in data) {
NSLog(@"image rc: %d", [image retainCount]); // always prints rc = 1
}
[data removeAllObjects];
}
+ (UIImageCached *)imageFromPath:(NSString *)imagePath {
UIImageCached *image = (UIImageCached*)[self.cache objectForKey:imagePath];
if (image == nil) {
NSData *imageData = [[NSData alloc] initWithContentsOfFile:imagePath options:NSDataReadingMappedIfSafe error:nil];
image = (UIImageCached*)[UIImageCached imageWithData:imageData];
[imageData release];
if (image) {
[self.cache setObject:image forKey:imagePath];
//NSLog(@"new cached image: #%d", self.cache.count);
} else {
//NSLog(@"can't cache image: #%d", self.cache.count);
}
}
return image;
}
+ (UIImageCached *)imageNamed:(NSString *)imageName {
NSString *extension = [imageName pathExtension];
NSString *fileName = [imageName stringByDeletingPathExtension];
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:fileName ofType:extension];
return [self imageFromPath:fileLocation];
}
+ (UIImageCached *)retinaImageNamed:(NSString *)imageName {
UIImageCached *image = (UIImageCached*)[self.cache objectForKey:imageName];
if (image == nil) {
NSString *extension = [imageName pathExtension];
NSString *fileName = [imageName stringByDeletingPathExtension];
float s = 1.0;
// retina filename support
if(!isIPAD && [[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
s = [[UIScreen mainScreen] scale];
if (s > 1)
fileName = NSTR2(@"%@%@", fileName, @"@2x");
}
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:fileName ofType:extension];
NSData *imgData = [[NSData alloc] initWithContentsOfFile:fileLocation options:NSDataReadingMappedIfSafe error:nil];
BaseUIimage *tmpImage = [[BaseUIimage alloc] initWithData:imgData];
[imgData release];
image = (UIImageCached*)[UIImageCached imageWithCGImage:tmpImage.CGImage
scale:s
orientation:UIImageOrientationUp];
[tmpImage release];
if (image) {
[self.cache setObject:image forKey:imageName];
//NSLog(@"-- CACHE: new cached image: #%d", self.cache.count);
} else {
NSLog(@"-- CACHE: can't cache image: %@", fileLocation);
}
} else {
//NSLog(@"-- CACHE: read cached image");
}
return image;
}
@end
為什么從不調用release和dealloc? 這是否意味着我創建的UIImage實例沒有被取消分配,這就是虛擬內存增加的原因嗎?
Dave Wood幾乎是正確的(他的實現仍將創建UIImage)。 問題是
[UIImage imageWithData:data];
將創建一個UIImage而不是您希望它創建的子類。 要對此進行測試,請嘗試以下imageWithData實現:
+ (id)imageWithData:(NSData *)data {
NSLog(@"UIImage imageWithData");
UIImage *im = [UIImage imageWithData:data];
NSLog(@"%@",[im class]);
return im;
}
NSLog將輸出UIImage。 不是您所需要的。 我建議以下實現:
+ (id)imageWithData:(NSData *)data {
NSLog(@"UIImage imageWithData");
BaseUIimage *im = [[BaseUIimage alloc] initWithData:data];
return [im autorelease];
}
這將創建BaseUIimage類的圖像,並因此表現出您所期望的。 干杯。
您創建的子類錯誤。 在這里的代碼中:
@implementation BaseUIimage
+ (id)imageWithData:(NSData *)data {
NSLog(@"UIImage imageWithData");
return [UIImage imageWithData:data];
}
...
您沒有獲得BaseUIimage的實例,而是獲得了常規的UIImage,這意味着未調用覆蓋的release / dealloc等,因為它們不屬於UIImage類。
您需要將該函數更改為:
@implementation BaseUIimage
+ (id)imageWithData:(NSData *)data {
NSLog(@"UIImage imageWithData");
return [super imageWithData:data];
}
...
這將返回您的BaseUIimage類的實例。 現在,您將能夠看到被覆蓋的方法被調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.