Advertisement
Guest User

IconMaker.m

a guest
Apr 10th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "IconMaker.h"
  2.  
  3. #define SIZES @[@16, @32, @128, @256, @512]
  4.  
  5. @implementation IconMaker {
  6.     NSImage *actImage;
  7.     NSString *iconSet;
  8. }
  9.  
  10. - (NSString*)createIconFromImage:(NSImage*)image {
  11.     actImage = image;
  12.    
  13.     /*
  14.      * Create Iconset
  15.      */
  16.    
  17.     [self createIconSet];
  18.    
  19.     /*
  20.      * Resize Icons
  21.      */
  22.    
  23.     NSImageRep *imageRep = [[image representations] objectAtIndex:0];
  24.     NSSize imageSize = NSMakeSize(imageRep.pixelsWide, imageRep.pixelsHigh);
  25.    
  26.     //check what's the biggest possible size
  27.     int biggestSize = [self getBiggestSize:imageSize];
  28.    
  29.     /*
  30.      * Resize image
  31.      */
  32.    
  33.     for (int i=0; i<=biggestSize; i++) {
  34.         //create normal size
  35.         int size = [SIZES[i] intValue];
  36.         NSImage *resizedImage = [self resizeImage:size];
  37.         [resizedImage lockFocus];
  38.         NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0.0, 0.0, resizedImage.size.width, resizedImage.size.height)];
  39.         [resizedImage unlockFocus];
  40.         NSData *imageData = [imageRep representationUsingType:NSPNGFileType properties:nil];
  41.         NSString *fileName = [iconSet stringByAppendingFormat:@"/icon_%ix%i.png", size, size];
  42.         [imageData writeToFile:fileName atomically:NO];
  43.        
  44.         //create retina size
  45.         //actually we don't need to ... ?! but iconutil will throw warnings
  46.     }
  47.    
  48.     /*
  49.      * Create icns
  50.      */
  51.    
  52.     NSString *icns = [self createIcns];
  53.    
  54.     /*
  55.      * Remove Iconset
  56.      */
  57.    
  58.     [self removeIconSet];
  59.    
  60.     /*
  61.      * return
  62.      */
  63.     return icns;
  64. }
  65.  
  66. #pragma mark - Iconset
  67.  
  68. - (void)createIconSet {
  69.     iconSet = [NSTemporaryDirectory() stringByAppendingString:@"IconMaker.iconset"];
  70.     [[NSFileManager defaultManager] createDirectoryAtPath:iconSet withIntermediateDirectories:NO attributes:nil error:nil];
  71. }
  72.  
  73. - (void)removeIconSet {
  74.     [[NSFileManager defaultManager] removeItemAtPath:iconSet error:nil];
  75. }
  76.  
  77. #pragma mark - Size
  78.  
  79. - (int)getBiggestSize:(NSSize)size {
  80.     for (int i=(int)SIZES.count-1; i>0; i--) {
  81.         if ((int)size.width >= [SIZES[i] intValue])
  82.             return i;
  83.     }
  84.     return 0;
  85. }
  86.  
  87. #pragma mark - Resizing
  88.  
  89. - (NSImage*)resizeImage:(int)size {
  90.     //retina?
  91.     CGFloat screenScale = [[NSScreen mainScreen] backingScaleFactor];
  92.     size = size / screenScale;
  93.    
  94.     NSImage *sourceImage = actImage;
  95.    
  96.     NSSize newSize = NSMakeSize(size, size);
  97.     NSImage *resizedImage = [[NSImage alloc] initWithSize:newSize];
  98.    
  99.     NSSize originalSize = [sourceImage size];
  100.    
  101.     [resizedImage lockFocus];
  102.     [sourceImage drawInRect:NSMakeRect(0, 0, size, size) fromRect:NSMakeRect(0, 0, originalSize.width, originalSize.height) operation:NSCompositeSourceOver fraction:1.0];
  103.     [resizedImage unlockFocus];
  104.    
  105.     return resizedImage;
  106. }
  107.  
  108. #pragma mark - make icns
  109.  
  110. - (NSString*)createIcns {
  111.     NSTask *task = [[NSTask alloc] init];
  112.     task.launchPath = @"/usr/bin/iconutil";
  113.     task.arguments = @[@"-c", @"icns", iconSet];
  114.     task.standardError = [NSPipe pipe];
  115.     [task launch];
  116.     [task waitUntilExit];   //otherwise the above method will delete the iconset before finishing creating the .icns file
  117.    
  118.     return [[iconSet stringByDeletingPathExtension] stringByAppendingString:@".icns"];
  119. }
  120.  
  121. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement