我正在创建一个 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 类对象存储在数组中。并显示在表格 View 中。
编辑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 ;
}
最佳答案
这不会导致我的代码中出现内存泄漏 - 使用泄漏/分配进行检查:
GAppSettings.h
你的 WEFile 类
NSImage *gAppIcon;
@property (readwrite, retain) NSImage *gAppIcon;
*.m @synthesize gAppIcon;
在 ->init
gAppIcon = nil;
在 -> dealloc
[ gAppIcon release];
我还用于存储和恢复
->initWithCoder
self.gAppIcon = [ decoder decodeObjectForKey:@"gAppIcon"];
->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;
然后添加到数组中...
关于objective-c - cocoa [[NSWorkspace 共享工作空间] iconForFile :filePath caused memory leaks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22841194/