[英]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.