groups

Link to this snippet:


Download to Code Collector

language: Objective-C
licence: Other

NSImage Create QuickLook icon for URL

options: send to code collectorview all myztikjenz's snippets
#import <QuickLook/QLThumbnailImage.h>

@implementation NSImage (NSImageAdditions)

+(NSImage *) quicklookIconForURL:(NSURL *)url ofSize:(NSSize)aSize iconMode:(BOOL)iconMode
{
	NSImage *image = nil;
	
	if( url && [[NSFileManager defaultManager] fileExistsAtPath:[url path]] )
	{
		CFMutableDictionaryRef options;
		if( iconMode )
		{
			options = CFDictionaryCreateMutable( kCFAllocatorDefault, 0, NULL, NULL );
			CFDictionaryAddValue( options, kQLThumbnailOptionIconModeKey, kCFBooleanTrue );
		}
		
		if( NSEqualSizes( aSize, NSZeroSize ) )
			aSize = NSMakeSize( 128.0, 128.0 );
		
		CGImageRef thumbnailRef = QLThumbnailImageCreate( kCFAllocatorDefault, (CFURLRef)url, NSSizeToCGSize( aSize ), options );
		
		if( thumbnailRef )
		{
			NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithCGImage:thumbnailRef];
			image = [[[NSImage alloc] init] autorelease];
			[image addRepresentation:imageRep];
			
			[imageRep release];
			
			CFRelease( thumbnailRef );
		}
		
		if( options )
			CFRelease( options );
	}
	
	return( image );
}

@end