Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#295 closed Enhancement (invalid)

Use less memory for icons

Reported by: cmCM Owned by: livings124
Priority: Normal Milestone:
Component: Mac Client Version: 0.80
Severity: Normal Keywords:
Cc:

Description

NSWorkspace usually returns an icon as an opened ICNS file, thus with all the image representations. With Leopard, these representations are up to 512x512 and take a considerable amount of memory (try to save it with NSKeyedArchiver - it's hundreds of kB's). Here is a small method that returns an image with 1 representation only and in wanted size:

-(NSImage*)imageWithSingleImageRepOfSize:(NSSize)size{

if (NSEqualSizes(NSZeroSize, size)){

return nil;

}

NSImage *icon; NSSize s = [self size];

icon = [[[NSImage alloc] initWithSize:size] autorelease]; [icon lockFocus];

float height = (s.height>s.width)?size.height:(size.width/s.width)*s.height; float width = (s.width>=s.height)?size.width:(size.height/s.height)*s.width;

[self drawInRect:NSMakeRect((size.width - width)/2, (size.height - height)/2, width, height) fromRect:NSMakeRect(0, 0, s.width, s.height) operation:NSCompositeCopy fraction:1.0];

[icon unlockFocus];

if ([[icon representations] count]>1
[[icon representations] count]==0){

NSLog(@"image scaled with more than one rep or with none: %i", [[icon representations] count]); something went wrong return self;

}

return icon;

}

It's meant to be a NSImage additions, so you can use it e.g. [[[NSWorkspace sharedWorkspace] iconForFile:path] imageWithSingleImageRepOfSize:NSMakeSize(16.0, 16.0)]; - for an icon 16x16 - those images take a few kB's of memory.

Change History (2)

comment:1 Changed 12 years ago by livings124

  • Milestone Sometime deleted
  • Resolution set to invalid
  • Status changed from new to closed

The only image that is really stored is the torrent file image, and it is used at multiple sizes. I do appreciate the code and might use it when needed, but there is no need to keep this as an open ticket.

comment:2 Changed 12 years ago by livings124

I wish there was a more appropriate resolution value in this trac...

Note: See TracTickets for help on using tickets.