1

我正在创建一个可可应用程序,我需要在其中找出文件/目录的图标图像。我正在使用它来获取给定路径的图标图像。

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 类对象存储在一个数组中。并显示在表格视图中。

编辑 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 ;

}
4

1 回答 1

0

这在我的代码中不会导致内存泄漏 - 使用 Leaks/Allocations 检查它:

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;

然后添加到数组中...

于 2014-04-04T10:32:52.083 回答