Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ImageProcessor.m
- // gulfstream
- //
- // Created by Andres David Carreño on 8/22/12.
- // Copyright (c) 2012 Zemoga. All rights reserved.
- //
- #import "ImageProcessor.h"
- @implementation ImageProcessor
- + (BOOL)createFaceWithParameters:(NSString*)paramsString fromPath:(NSString*)path cubeFace:(NSString*)face forDebugging:(BOOL)forDebugging{
- BOOL boolResult = NO;
- NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
- NSString *bundle = @"designer.bundle";
- NSString *bundlePath = [NSString stringWithFormat:@"%@/%@",documentsPath,bundle];
- //Creating arrays from the incoming strings
- //NSMutableArray *imagesArray = [[NSMutableArray alloc]initWithArray:[imagesString componentsSeparatedByString:@","]];
- NSMutableArray *paramsArray = [[NSMutableArray alloc]initWithArray:[paramsString componentsSeparatedByString:@","]];
- //We are going to store the UIImage instances here
- NSMutableArray *uiImagesArray = [[NSMutableArray alloc]init];
- for (int i=0; i<[imagesArray count]; i++) {
- if (![[paramsArray objectAtIndex:i] isEqualToString:@""]){
- //Creating Image using path and nameFile.
- NSString *imagePath = [NSString stringWithString:[NSString stringWithFormat:@"%@/%@/%@.png"
- ,bundlePath
- ,path
- ,[imagesArray objectAtIndex:i]]];
- UIImage *image = [[UIImage alloc]initWithContentsOfFile:imagePath];
- //Creating Color From Parameters
- NSString *rgbaColor = [paramsArray objectAtIndex:i];
- NSArray *rgbaArray = [rgbaColor componentsSeparatedByString:@"-"];
- NSMutableArray *finalArray = [[NSMutableArray alloc]init];
- CGFloat rComponent;
- CGFloat gComponent;
- CGFloat bComponent;
- CGFloat aComponent;
- rComponent = [[rgbaArray objectAtIndex:0] floatValue]/255.0;
- gComponent = [[rgbaArray objectAtIndex:1] floatValue]/255.0;
- bComponent = [[rgbaArray objectAtIndex:2] floatValue]/255.0;
- aComponent = [[rgbaArray objectAtIndex:3] floatValue];
- UIColor *color = [UIColor colorWithRed:rComponent green:gComponent blue:bComponent alpha:aComponent];
- // this could be processed inside a block with GCD
- //Colorize Image
- UIImage *actualImage = [ImageProcessor colorizeImage:image withColor:color];
- [finalArray addObject:actualImage];
- //Shadows Image
- UIImage *shadowsImage = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/%@_shadows.png"
- ,bundlePath
- ,path,[imagesArray objectAtIndex:i]]];
- [finalArray addObject:shadowsImage];
- // this could be processed inside a block with GCD
- UIImage *finalImage = [ImageProcessor mergeImagesFromArray:finalArray];
- [uiImagesArray addObject:finalImage];
- }else{
- NSString *stringTest = [NSString stringWithFormat:@"%@/%@/%@.png",bundlePath,path,[imagesArray objectAtIndex:i]];
- UIImage *image = [[UIImage alloc]initWithContentsOfFile:stringTest];
- [uiImagesArray addObject:image];
- }
- }
- UIImage *imageFromMerge = [ImageProcessor mergeImagesFromArray:uiImagesArray];
- NSData *pngData = UIImagePNGRepresentation(imageFromMerge);
- NSString *newDir = [NSString stringWithFormat:@"%@/../tmp/", [[NSBundle mainBundle] bundlePath]];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- [fileManager createDirectoryAtPath: newDir withIntermediateDirectories:YES attributes: nil error:nil];
- if([fileManager createFileAtPath:[NSString stringWithFormat:@"%@%@.png",newDir,face] contents:pngData attributes:nil]){
- boolResult = YES;
- }
- NSLog(@"RESULT:%d", boolResult);
- return boolResult;
- }
- + (UIImage*)mergeImagesFromArray : (NSArray*) arrayOfImagesLocal {
- NSLog(@"=============== IMAGES MERGED ===============");
- UIImage *resultImage = [[UIImage alloc]init];
- if ([arrayOfImagesLocal count] != 0) {
- CGSize newSize = CGSizeMake(512, 512);
- UIGraphicsBeginImageContext(newSize);
- for (UIImage *currentImage in arrayOfImagesLocal) {
- [currentImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];
- }
- resultImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return resultImage;
- }
- return resultImage;
- }
- + (UIImage *)colorizeImage:(UIImage *)baseImage withColor:(UIColor *)color {
- UIGraphicsBeginImageContext(baseImage.size);
- CGContextRef ctx = UIGraphicsGetCurrentContext();
- CGRect area = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height);
- CGContextScaleCTM(ctx, 1, -1);
- CGContextTranslateCTM(ctx, 0, -area.size.height);
- CGContextSaveGState(ctx);
- CGContextClipToMask(ctx, area, baseImage.CGImage);
- [color set];
- CGContextFillRect(ctx, area);
- CGContextRestoreGState(ctx);
- CGContextSetBlendMode(ctx, kCGBlendModeOverlay);
- CGContextDrawImage(ctx, area, baseImage.CGImage);
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return newImage;
- }
- @end
Add Comment
Please, Sign In to add comment