Created
July 28, 2017 14:21
-
-
Save joncardasis/0bdb2c46ed8bb8333b249e373599cca5 to your computer and use it in GitHub Desktop.
Command line Cocoa program which overlays a png over another image - great for image banners on app icons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ImageMerger | |
// | |
// Created by Cardasis, Jonathan (J.) on 4/15/16. | |
// Copyright © 2016 Cardasis, Jonathan (J.). All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import <Cocoa/Cocoa.h> | |
NSImage* mergeImages(NSImage *bottom, NSImage *overlay, CGFloat overlayAlpha){ | |
NSImage *newImage = [[NSImage alloc] initWithSize:[bottom size]]; | |
[newImage lockFocus]; | |
CGRect newImageRect = CGRectZero; | |
newImageRect.size = [newImage size]; | |
[bottom drawInRect:newImageRect]; | |
if(overlayAlpha < 1){ | |
[overlay drawInRect:newImageRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:overlayAlpha];//draw with opacity | |
}else{ | |
[overlay drawInRect:newImageRect]; | |
} | |
[newImage unlockFocus]; | |
return newImage; | |
} | |
bool saveImageToFilepath(NSImage *image, NSString *path){ | |
CGImageRef imgRef = [image CGImageForProposedRect:NULL context:nil hints:nil]; | |
NSBitmapImageRep *imgRep = [[NSBitmapImageRep alloc] initWithCGImage:imgRef]; | |
NSData *imgData = [imgRep representationUsingType:NSPNGFileType properties:@{}]; | |
return [imgData writeToFile:path atomically:YES]; | |
} | |
void printUsage(const char *appname){ | |
fprintf(stderr, "Proper Usage:\n"); | |
fprintf(stderr, "\t%s /path/to/oldAppIcon.png /path/to/overlayImage.png \n", appname); | |
} | |
//setIconBadge oldImg.png overlayImg.png | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
if(argc != 3){//2 arguments | |
NSString *argZero = [NSString stringWithFormat:@"%s", argv[0]]; | |
const char *appname = [[[argZero lastPathComponent] stringByDeletingPathExtension] cStringUsingEncoding:NSUTF8StringEncoding]; | |
printUsage(appname); | |
return 1; | |
} | |
NSArray *arguments = [[NSProcessInfo processInfo] arguments]; | |
// NSFileManager *fileManager = [[NSFileManager alloc] init]; | |
// NSString *currentPath = [fileManager currentDirectoryPath]; | |
NSString *oldIconFilepath = arguments[1]; | |
NSString *overlayFilepath = arguments[2]; | |
NSImage *base = [[NSImage alloc] initByReferencingFile:oldIconFilepath]; | |
NSImage *overlay = [[NSImage alloc] initByReferencingFile:overlayFilepath]; | |
NSImage *finalIcon = mergeImages(base, overlay, 1.0); | |
bool success = saveImageToFilepath(finalIcon, oldIconFilepath);//save over old app icon | |
if(!success){ | |
fprintf(stderr, "A problem occured replacing the app icons.\n"); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment