// // ZGImageUtils.m // UUTUtils // // Created by zhuge on 2017/12/28. // Copyright © 2017年 zhuge. All rights reserved. // #import "ZGImageUtils.h" @implementation ZGImageUtils + (void)resizePng:(NSString *)path toWidth:(int)width toHeight:(int)height { [ZGImageUtils resizePng:path toWidth:width toHeight:height toPath:path]; } + (void)resizePng:(NSString *)path toWidth:(int)width toHeight:(int)height toPath:(NSString *)toPath { NSString *parentPath = [toPath stringByDeletingLastPathComponent]; NSFileManager *fm = [NSFileManager defaultManager]; if (![fm fileExistsAtPath:parentPath]) { [fm createDirectoryAtPath:parentPath withIntermediateDirectories:YES attributes:nil error:nil]; } NSString *cmd = @"/usr/bin/sips"; NSArray *args = @[ @"--resampleHeightWidth", [NSString stringWithFormat:@"%d", height], [NSString stringWithFormat:@"%d", width], path, @"--out", toPath ]; NSTask *currentTask = [[NSTask alloc] init]; [currentTask setLaunchPath:cmd]; NSMutableDictionary *environmentDict = [NSMutableDictionary dictionaryWithDictionary:[[NSProcessInfo processInfo] environment]]; NSString *sysPath = [environmentDict objectForKey:@"PATH"]; sysPath = [NSString stringWithFormat:@"%@:%@", [[NSBundle mainBundle] pathForResource:@"ApkTools" ofType:nil], sysPath]; [environmentDict setValue:sysPath forKey:@"PATH"]; [environmentDict setValue:@"-Dfile.encoding=UTF-8" forKey:@"JAVA_TOOL_OPTIONS"]; [currentTask setEnvironment:environmentDict]; // Set arguments [currentTask setArguments:args]; // Set output [currentTask setStandardOutput:[NSFileHandle fileHandleWithStandardOutput]]; [currentTask setStandardError:[NSFileHandle fileHandleWithStandardError]]; [currentTask launch]; [currentTask waitUntilExit]; } @end