ZGImageUtils.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // ZGImageUtils.m
  3. // UUTUtils
  4. //
  5. // Created by zhuge on 2017/12/28.
  6. // Copyright © 2017年 zhuge. All rights reserved.
  7. //
  8. #import "ZGImageUtils.h"
  9. @implementation ZGImageUtils
  10. + (void)resizePng:(NSString *)path toWidth:(int)width toHeight:(int)height {
  11. [ZGImageUtils resizePng:path toWidth:width toHeight:height toPath:path];
  12. }
  13. + (void)resizePng:(NSString *)path toWidth:(int)width toHeight:(int)height toPath:(NSString *)toPath {
  14. NSString *parentPath = [toPath stringByDeletingLastPathComponent];
  15. NSFileManager *fm = [NSFileManager defaultManager];
  16. if (![fm fileExistsAtPath:parentPath]) {
  17. [fm createDirectoryAtPath:parentPath withIntermediateDirectories:YES attributes:nil error:nil];
  18. }
  19. NSString *cmd = @"/usr/bin/sips";
  20. NSArray *args = @[
  21. @"--resampleHeightWidth",
  22. [NSString stringWithFormat:@"%d", height],
  23. [NSString stringWithFormat:@"%d", width],
  24. path,
  25. @"--out",
  26. toPath
  27. ];
  28. NSTask *currentTask = [[NSTask alloc] init];
  29. [currentTask setLaunchPath:cmd];
  30. NSMutableDictionary *environmentDict = [NSMutableDictionary dictionaryWithDictionary:[[NSProcessInfo processInfo] environment]];
  31. NSString *sysPath = [environmentDict objectForKey:@"PATH"];
  32. sysPath = [NSString stringWithFormat:@"%@:%@", [[NSBundle mainBundle] pathForResource:@"ApkTools" ofType:nil], sysPath];
  33. [environmentDict setValue:sysPath forKey:@"PATH"];
  34. [environmentDict setValue:@"-Dfile.encoding=UTF-8" forKey:@"JAVA_TOOL_OPTIONS"];
  35. [currentTask setEnvironment:environmentDict];
  36. // Set arguments
  37. [currentTask setArguments:args];
  38. // Set output
  39. [currentTask setStandardOutput:[NSFileHandle fileHandleWithStandardOutput]];
  40. [currentTask setStandardError:[NSFileHandle fileHandleWithStandardError]];
  41. [currentTask launch];
  42. [currentTask waitUntilExit];
  43. }
  44. @end