Guest User

Untitled

a guest
Dec 10th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. //
  2. // ImageProcessor.m
  3. // gulfstream
  4. //
  5. // Created by Andres David Carreño on 8/22/12.
  6. // Copyright (c) 2012 Zemoga. All rights reserved.
  7. //
  8.  
  9. #import "ImageProcessor.h"
  10.  
  11. @implementation ImageProcessor
  12.  
  13. + (BOOL)createFaceWithParameters:(NSString*)paramsString fromPath:(NSString*)path cubeFace:(NSString*)face forDebugging:(BOOL)forDebugging{
  14.  
  15. BOOL boolResult = NO;
  16. NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  17. NSString *bundle = @"designer.bundle";
  18. NSString *bundlePath = [NSString stringWithFormat:@"%@/%@",documentsPath,bundle];
  19.  
  20. //Creating arrays from the incoming strings
  21. //NSMutableArray *imagesArray = [[NSMutableArray alloc]initWithArray:[imagesString componentsSeparatedByString:@","]];
  22. NSMutableArray *paramsArray = [[NSMutableArray alloc]initWithArray:[paramsString componentsSeparatedByString:@","]];
  23.  
  24. //We are going to store the UIImage instances here
  25. NSMutableArray *uiImagesArray = [[NSMutableArray alloc]init];
  26.  
  27. for (int i=0; i<[imagesArray count]; i++) {
  28. if (![[paramsArray objectAtIndex:i] isEqualToString:@""]){
  29.  
  30. //Creating Image using path and nameFile.
  31. NSString *imagePath = [NSString stringWithString:[NSString stringWithFormat:@"%@/%@/%@.png"
  32. ,bundlePath
  33. ,path
  34. ,[imagesArray objectAtIndex:i]]];
  35.  
  36. UIImage *image = [[UIImage alloc]initWithContentsOfFile:imagePath];
  37. //Creating Color From Parameters
  38. NSString *rgbaColor = [paramsArray objectAtIndex:i];
  39. NSArray *rgbaArray = [rgbaColor componentsSeparatedByString:@"-"];
  40. NSMutableArray *finalArray = [[NSMutableArray alloc]init];
  41.  
  42. CGFloat rComponent;
  43. CGFloat gComponent;
  44. CGFloat bComponent;
  45. CGFloat aComponent;
  46.  
  47. rComponent = [[rgbaArray objectAtIndex:0] floatValue]/255.0;
  48. gComponent = [[rgbaArray objectAtIndex:1] floatValue]/255.0;
  49. bComponent = [[rgbaArray objectAtIndex:2] floatValue]/255.0;
  50. aComponent = [[rgbaArray objectAtIndex:3] floatValue];
  51.  
  52. UIColor *color = [UIColor colorWithRed:rComponent green:gComponent blue:bComponent alpha:aComponent];
  53.  
  54. // this could be processed inside a block with GCD
  55. //Colorize Image
  56. UIImage *actualImage = [ImageProcessor colorizeImage:image withColor:color];
  57. [finalArray addObject:actualImage];
  58.  
  59. //Shadows Image
  60. UIImage *shadowsImage = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/%@_shadows.png"
  61. ,bundlePath
  62. ,path,[imagesArray objectAtIndex:i]]];
  63.  
  64. [finalArray addObject:shadowsImage];
  65.  
  66. // this could be processed inside a block with GCD
  67. UIImage *finalImage = [ImageProcessor mergeImagesFromArray:finalArray];
  68.  
  69. [uiImagesArray addObject:finalImage];
  70.  
  71.  
  72. }else{
  73. NSString *stringTest = [NSString stringWithFormat:@"%@/%@/%@.png",bundlePath,path,[imagesArray objectAtIndex:i]];
  74. UIImage *image = [[UIImage alloc]initWithContentsOfFile:stringTest];
  75.  
  76. [uiImagesArray addObject:image];
  77. }
  78. }
  79.  
  80. UIImage *imageFromMerge = [ImageProcessor mergeImagesFromArray:uiImagesArray];
  81.  
  82. NSData *pngData = UIImagePNGRepresentation(imageFromMerge);
  83.  
  84. NSString *newDir = [NSString stringWithFormat:@"%@/../tmp/", [[NSBundle mainBundle] bundlePath]];
  85.  
  86. NSFileManager *fileManager = [NSFileManager defaultManager];
  87.  
  88. [fileManager createDirectoryAtPath: newDir withIntermediateDirectories:YES attributes: nil error:nil];
  89.  
  90. if([fileManager createFileAtPath:[NSString stringWithFormat:@"%@%@.png",newDir,face] contents:pngData attributes:nil]){
  91. boolResult = YES;
  92. }
  93.  
  94. NSLog(@"RESULT:%d", boolResult);
  95. return boolResult;
  96. }
  97.  
  98. + (UIImage*)mergeImagesFromArray : (NSArray*) arrayOfImagesLocal {
  99. NSLog(@"=============== IMAGES MERGED ===============");
  100. UIImage *resultImage = [[UIImage alloc]init];
  101. if ([arrayOfImagesLocal count] != 0) {
  102.  
  103. CGSize newSize = CGSizeMake(512, 512);
  104. UIGraphicsBeginImageContext(newSize);
  105.  
  106. for (UIImage *currentImage in arrayOfImagesLocal) {
  107. [currentImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];
  108. }
  109.  
  110. resultImage = UIGraphicsGetImageFromCurrentImageContext();
  111. UIGraphicsEndImageContext();
  112.  
  113. return resultImage;
  114. }
  115. return resultImage;
  116. }
  117.  
  118. + (UIImage *)colorizeImage:(UIImage *)baseImage withColor:(UIColor *)color {
  119.  
  120. UIGraphicsBeginImageContext(baseImage.size);
  121.  
  122. CGContextRef ctx = UIGraphicsGetCurrentContext();
  123. CGRect area = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height);
  124.  
  125. CGContextScaleCTM(ctx, 1, -1);
  126. CGContextTranslateCTM(ctx, 0, -area.size.height);
  127. CGContextSaveGState(ctx);
  128. CGContextClipToMask(ctx, area, baseImage.CGImage);
  129. [color set];
  130. CGContextFillRect(ctx, area);
  131. CGContextRestoreGState(ctx);
  132. CGContextSetBlendMode(ctx, kCGBlendModeOverlay);
  133. CGContextDrawImage(ctx, area, baseImage.CGImage);
  134. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  135. UIGraphicsEndImageContext();
  136. return newImage;
  137. }
  138.  
  139. @end
Add Comment
Please, Sign In to add comment