Skip to content

Instantly share code, notes, and snippets.

@puelocesar
Created February 9, 2016 14:26
Show Gist options
  • Save puelocesar/2be1c7249b4a4968ef0c to your computer and use it in GitHub Desktop.
Save puelocesar/2be1c7249b4a4968ef0c to your computer and use it in GitHub Desktop.
//
// main.m
// mudaImagens
//
// Created by Paulo Cesar on 09/02/16.
// Copyright © 2016 Paulo Cesar. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSString* dirPath = [NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding];
NSFileManager* manager = [NSFileManager defaultManager];
NSError* error;
NSArray* files = [manager contentsOfDirectoryAtPath:dirPath error:&error];
if (files) {
for (NSString* fileName in files) {
if ([fileName hasSuffix:@".png"]) {
NSString* imagePath = [NSString stringWithFormat:@"%@/%@", dirPath, fileName];
NSImage* image = [[NSImage alloc] initWithContentsOfFile:imagePath];
if (image.representations && image.representations.count > 0) {
long lastSquare = 0, curSquare;
NSImageRep *imageRep;
for (imageRep in image.representations) {
curSquare = imageRep.pixelsWide * imageRep.pixelsHigh;
if (curSquare > lastSquare) {
image.size = NSMakeSize(imageRep.pixelsWide, imageRep.pixelsHigh);
lastSquare = curSquare;
}
}
if (image.size.width != image.size.height) {
float maxSize = MAX(image.size.width, image.size.height);
NSSize finalSize;
finalSize.width = maxSize;
finalSize.height = maxSize;
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:maxSize
pixelsHigh:maxSize
bitsPerSample:16
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow:0
bitsPerPixel:0];
[rep setSize:finalSize];
NSRect rect = NSMakeRect((maxSize-image.size.width)/2,
(maxSize-image.size.height)/2,
image.size.width,
image.size.height);
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:rep]];
[image drawInRect:rect fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
NSData *data = [rep representationUsingType:NSPNGFileType properties:nil];
[data writeToFile:imagePath atomically:YES];
NSLog(@"mudou imagem: %@", fileName);
}
}
}
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment