我正在创建一个cocoa应用程序,我需要找到文件/目录的图标图像。我用它来获取给定路径的图标图像。
NSImage *image = [[NSWorkspace sharedWorkspace] iconForFile:path];
这种方法在整个应用中被隐性地称为。我使用我的应用程序增加了应用程序的分配内存。当我查找内存泄漏时使用仪器,我发现上述方法导致100%内存泄漏。 我怎样才能消除这些内存泄漏,或者是他们为什么我可以获得图标图像和内存的任何其他方式都不会成为问题。 请帮帮我一个人。 谢谢
编辑:
这是我调用此方法的方法。
-(WEFile *)fileAtPath:(NSString *)filePath
{
WEFile *file = [[WEFile alloc] init];
file.fIconImage = [workSpace iconForFile:filePath];
file.Name = [fileManager displayNameAtPath:filePath];
file.type = [workSpace localizedDescriptionForType:[workSpace typeOfFile:filePath error:&error]];
NSDictionary *detailDict = [fileManager attributesOfItemAtPath:filePath error:&error];
file.modificationDate = [ Utility createDateFormat:[detailDict objectForKey:NSFileModificationDate]];
file.creationDate = [ Utility createDateFormat:[detailDict objectForKey:NSFileCreationDate]];
file.Size = [[detailDict objectForKey:NSFileSize] integerValue];
file.fPath = filePath;
NSDictionary *metaDict = [self metadataForFileAtPath:filePath];
file.addedDate = [ Utility createDateFormat:[metaDict objectForKey:@"kMDItemDateAdded"]];
file.lastOpenedDate = [ Utility createDateFormat:[metaDict objectForKey:@"kMDItemLastUsedDate"]];
return [file autorelease];
}
方法fileAtPath从另一个方法递归调用,WEFile类对象存储在一个数组中。并在tableview中显示。
编辑2:这是我调用fileAtPath方法的代码。当表选择传递目录路径作为参数时,调用此方法directoryAtPath。
- (WEDirectory *)directoryAtPath:(NSString *)dirPath {
WEDirectory *dir = [[[WEDirectory alloc] init] autorelease];
NSArray *childArray = [self getContentsWithinDirectory:dirPath];
if (childArray && [childArray count]>0)
{
for (int i = 0; i<[childArray count]; i++)
{
NSURL *fileURL = [childArray objectAtIndex:i];
NSString *filePath = [self getPathFromURL:fileURL];
filePath = [filePath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
BOOL isDir;
BOOL success = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
if (!success)
{
continue;
}
if(isDir)
{
WEDirectory *childDir = [[WEDirectory alloc] init] ;
childDir.dIconImage = [workSpace iconForFile:filePath];
childDir.Name = [fileManager displayNameAtPath:filePath];
childDir.type = [workSpace localizedDescriptionForType:[workSpace typeOfFile:dirPath error:&error]];
NSDictionary *detailDict = [fileManager attributesOfItemAtPath:filePath error:&error];
childDir.modificationDate = [ Utility createDateFormat:[detailDict objectForKey:NSFileModificationDate]];
childDir.creationDate = [ Utility createDateFormat:[detailDict objectForKey:NSFileCreationDate]];
childDir.Size = [[detailDict objectForKey:NSFileSize]integerValue];
childDir.dPath = filePath;
[dir.childsArray addObject:childDir];
[childDir release];
}
else
{
WEFile *childFile = [self fileAtPath:filePath];
[dir.childsArray addObject:childFile];
}
}
}
return dir ;
}
答案 0 :(得分:0)
这可以在我的代码中不会导致内存泄漏 - 使用Leaks / Allocations检查它:
GAppSettings.h
您的WEFile级
NSImage *gAppIcon;
@property (readwrite, retain) NSImage *gAppIcon;
*。米 @synthesize gAppIcon;
in - &gt; init
gAppIcon = nil;
in - &gt; dealloc
[ gAppIcon release];
我也用于存储和恢复
- &GT; initWithCoder
self.gAppIcon = [ decoder decodeObjectForKey:@"gAppIcon"];
- &GT; encodeWithCoder
[ coder encodeObject: [ self gAppIcon] forKey: @"gAppIcon"];
和
- ( id ) copyWithZone: ( NSZone *)zone
{
return self;
}
图标经常在另一个类中加载:
GAppSettings *appSettings = [ [ [GAppSettings alloc] init] autorelease];
...
NSImage *appIcon = [ [ NSWorkspace sharedWorkspace] iconForFile:completeAppPath];
[ appSettings setGAppIcon:appIcon];
...
return appSettings;
然后添加到数组......