Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "IconMaker.h"
- #define SIZES @[@16, @32, @128, @256, @512]
- @implementation IconMaker {
- NSImage *actImage;
- NSString *iconSet;
- }
- - (NSString*)createIconFromImage:(NSImage*)image {
- actImage = image;
- /*
- * Create Iconset
- */
- [self createIconSet];
- /*
- * Resize Icons
- */
- NSImageRep *imageRep = [[image representations] objectAtIndex:0];
- NSSize imageSize = NSMakeSize(imageRep.pixelsWide, imageRep.pixelsHigh);
- //check what's the biggest possible size
- int biggestSize = [self getBiggestSize:imageSize];
- /*
- * Resize image
- */
- for (int i=0; i<=biggestSize; i++) {
- //create normal size
- int size = [SIZES[i] intValue];
- NSImage *resizedImage = [self resizeImage:size];
- [resizedImage lockFocus];
- NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0.0, 0.0, resizedImage.size.width, resizedImage.size.height)];
- [resizedImage unlockFocus];
- NSData *imageData = [imageRep representationUsingType:NSPNGFileType properties:nil];
- NSString *fileName = [iconSet stringByAppendingFormat:@"/icon_%ix%i.png", size, size];
- [imageData writeToFile:fileName atomically:NO];
- //create retina size
- //actually we don't need to ... ?! but iconutil will throw warnings
- }
- /*
- * Create icns
- */
- NSString *icns = [self createIcns];
- /*
- * Remove Iconset
- */
- [self removeIconSet];
- /*
- * return
- */
- return icns;
- }
- #pragma mark - Iconset
- - (void)createIconSet {
- iconSet = [NSTemporaryDirectory() stringByAppendingString:@"IconMaker.iconset"];
- [[NSFileManager defaultManager] createDirectoryAtPath:iconSet withIntermediateDirectories:NO attributes:nil error:nil];
- }
- - (void)removeIconSet {
- [[NSFileManager defaultManager] removeItemAtPath:iconSet error:nil];
- }
- #pragma mark - Size
- - (int)getBiggestSize:(NSSize)size {
- for (int i=(int)SIZES.count-1; i>0; i--) {
- if ((int)size.width >= [SIZES[i] intValue])
- return i;
- }
- return 0;
- }
- #pragma mark - Resizing
- - (NSImage*)resizeImage:(int)size {
- //retina?
- CGFloat screenScale = [[NSScreen mainScreen] backingScaleFactor];
- size = size / screenScale;
- NSImage *sourceImage = actImage;
- NSSize newSize = NSMakeSize(size, size);
- NSImage *resizedImage = [[NSImage alloc] initWithSize:newSize];
- NSSize originalSize = [sourceImage size];
- [resizedImage lockFocus];
- [sourceImage drawInRect:NSMakeRect(0, 0, size, size) fromRect:NSMakeRect(0, 0, originalSize.width, originalSize.height) operation:NSCompositeSourceOver fraction:1.0];
- [resizedImage unlockFocus];
- return resizedImage;
- }
- #pragma mark - make icns
- - (NSString*)createIcns {
- NSTask *task = [[NSTask alloc] init];
- task.launchPath = @"/usr/bin/iconutil";
- task.arguments = @[@"-c", @"icns", iconSet];
- task.standardError = [NSPipe pipe];
- [task launch];
- [task waitUntilExit]; //otherwise the above method will delete the iconset before finishing creating the .icns file
- return [[iconSet stringByDeletingPathExtension] stringByAppendingString:@".icns"];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement