Advertisement
Guest User

Untitled

a guest
May 8th, 2012
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Phonegap Prompt Plugin
  2. // Copyright (c) Paul Panserrieu, Zenexity 2011
  3. // MIT Licensed
  4.  
  5. #import "imagePicker.h"
  6.  
  7. @implementation imagePicker
  8.  
  9. - (void) show:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
  10.    
  11.     AGImagePickerController *imagePickerController = [[AGImagePickerController alloc] initWithFailureBlock:^(NSError *error) {
  12.        
  13.         if (error == nil)
  14.         {
  15.             NSLog(@"User has cancelled.");
  16.             [[self viewController] dismissModalViewControllerAnimated:YES];
  17.         } else
  18.         {    
  19.             NSLog(@"Error: %@", error);
  20.            
  21.             // Wait for the view controller to show first and hide it after that
  22.             double delayInSeconds = 0.5;
  23.             dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
  24.             dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  25.                 [[self viewController] dismissModalViewControllerAnimated:YES];
  26.             });
  27.         }
  28.        
  29.         [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
  30.        
  31.     } andSuccessBlock:^(NSArray *info) {        
  32.        
  33.         NSUInteger count = [info count];
  34.         int i = 0;
  35.         NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  36.         NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
  37.         NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"captured_photos"];
  38.  
  39.         for (i = 0; i < count; i++) {
  40.            
  41.             NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  42.             [dateFormatter setDateFormat:@"yyMMddHHmmss"];
  43.             NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
  44.             [dateFormatter release];
  45.             int randomValue = arc4random() % 1000;
  46.             NSString *uniqueStr = [NSString stringWithFormat:@"%@.%d",dateString,randomValue];
  47.            
  48.            
  49.             CLLocation * loc = [[info objectAtIndex: i] valueForProperty:ALAssetPropertyLocation];
  50.             if (loc == nil)
  51.             {
  52.              //   continue;
  53.             }
  54.            
  55.             NSDate * pNSDate = [[info objectAtIndex: i] valueForProperty:ALAssetPropertyDate];        
  56.            
  57.                        
  58.             ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation];
  59.             UIImage * image  = [UIImage imageWithCGImage:[rep fullResolutionImage]];
  60.  
  61.             NSString * orientation;
  62.             switch ([rep orientation]) {
  63.                 case UIImageOrientationUp:
  64.                     orientation = @"up";
  65.                     break;
  66.                 case UIImageOrientationDown:
  67.                     orientation = @"down";
  68.                     image = rotateUIImage(image, 180);
  69.                     break;
  70.                 case UIImageOrientationRight:
  71.                     orientation = @"right";
  72.                     image = rotateUIImage(image, 90);
  73.                     break;
  74.                 case UIImageOrientationLeft:
  75.                     orientation = @"left";
  76.                     image = rotateUIImage(image, -90);
  77.                     break;
  78.                 default:
  79.                     orientation = @"default";
  80.                     break;
  81.             }
  82.            
  83.            
  84.             NSInteger width  = image.size.width;
  85.             NSInteger height = image.size.height;
  86.  
  87.            
  88.             NSLog(@" image.size.width %f -- image.size.height %f " , image.size.width , image.size.height);
  89.            
  90.             if (image.size.width > 612)
  91.             {
  92.              
  93.                 image = [self imageByScalingAndCroppingForSize:image toSize:CGSizeMake(612,612*image.size.height/image.size.width)];
  94.                  NSLog(@" croping to ::  612*%f " , 612*image.size.height/image.size.width);
  95.             }
  96.            
  97.            
  98.             NSString * imageFileName = [@"/_" stringByAppendingFormat:@"%@_w_%d_h_%f_o_%@.jpg" ,uniqueStr , 612 , 612*image.size.height/image.size.width , orientation];
  99.             NSString* filePath =[dataPath stringByAppendingFormat:imageFileName];
  100.            
  101.            
  102.            
  103.            
  104.            
  105.             NSLog(@" pNSDate %@" , pNSDate);
  106.             NSString * timestamp = [NSString stringWithFormat:@"%.0f", [pNSDate timeIntervalSince1970]];
  107.             double timestampDB = [timestamp doubleValue];
  108.             int timestampint = (int)(timestampDB + (timestampDB>0 ? 0.5 : -0.5));
  109.            
  110.            
  111.            
  112.             NSDate *  nowNSDate = [NSDate date];
  113.             NSString * nowtimestampStr = [NSString stringWithFormat:@"%.0f", [nowNSDate timeIntervalSince1970]];
  114.             double nowtimestampDB = [nowtimestampStr doubleValue];
  115.             int nowtimestampint = (int)(nowtimestampDB + (nowtimestampDB>0 ? 0.5 : -0.5));
  116.  
  117.            
  118.            
  119.             NSData *dataImageForLenght = UIImageJPEGRepresentation(image , 100 / 100.0f);
  120.             if([dataImageForLenght writeToFile:filePath atomically:YES])
  121.             {
  122.                
  123.                 NSString * jsCallBack = [NSString stringWithFormat:
  124.                                          @"signal.apis.imageWasCaptured({path:'%@',mode:'cr',width:%d,height:%d,location:{timestamp: %.00f, \
  125.                                         coords:{latitude:%f,longitude:%f,altitude:%.02f,heading:%.02f,speed:%.02f,accuracy:%.02f, altitudeAccuracy:%.02f} \
  126.                                         }, createdSince:'%d' , orientation:'%@'})",
  127.                                          imageFileName , width , height,
  128.                                          [loc.timestamp timeIntervalSince1970] * 1000.0,
  129.                                          loc.coordinate.latitude,
  130.                                          loc.coordinate.longitude,
  131.                                          loc.altitude,
  132.                                          loc.course,
  133.                                          loc.speed,
  134.                                          loc.horizontalAccuracy,
  135.                                          loc.verticalAccuracy,
  136.                                          nowtimestampint-timestampint,
  137.                                          orientation
  138.                                          ];                
  139.                 [self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
  140.             }
  141.            
  142.            
  143.            
  144.             //            NSString *const ALAssetPropertyType;
  145.             //            NSString *const ALAssetPropertyLocation;
  146.             //            NSString *const ALAssetPropertyDuration;
  147.             //            NSString *const ALAssetPropertyOrientation;
  148.             //            NSString *const ALAssetPropertyDate;
  149.             //            NSString *const ALAssetPropertyRepresentations;
  150.             //            NSString *const ALAssetPropertyURLs;            
  151.         }
  152.        
  153.         NSString * jsCallBack2 = [NSString stringWithFormat:@"signal.app.goToPage('capturedPhotos_page');"];
  154.         [self.webView stringByEvaluatingJavaScriptFromString:jsCallBack2];
  155.        
  156.        
  157.         [[self viewController] dismissModalViewControllerAnimated:YES];
  158.         [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
  159.     }];
  160.    
  161.     [[self viewController] presentModalViewController:imagePickerController animated:YES];
  162.     [imagePickerController release];
  163. }
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. UIImage* rotateUIImage(const UIImage* src, float angleDegrees)  {  
  171.     UIView* rotatedViewBox = [[UIView alloc] initWithFrame: CGRectMake(0, 0, src.size.width, src.size.height)];
  172.     float angleRadians = angleDegrees * ((float)M_PI / 180.0f);
  173.     CGAffineTransform t = CGAffineTransformMakeRotation(angleRadians);
  174.     rotatedViewBox.transform = t;
  175.     CGSize rotatedSize = rotatedViewBox.frame.size;
  176.     [rotatedViewBox release];
  177.    
  178.     UIGraphicsBeginImageContext(rotatedSize);
  179.     CGContextRef bitmap = UIGraphicsGetCurrentContext();
  180.     CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
  181.     CGContextRotateCTM(bitmap, angleRadians);
  182.    
  183.     CGContextScaleCTM(bitmap, 1.0, -1.0);
  184.     CGContextDrawImage(bitmap, CGRectMake(-src.size.width / 2, -src.size.height / 2, src.size.width, src.size.height), [src CGImage]);
  185.    
  186.     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  187.     UIGraphicsEndImageContext();
  188.    
  189.     return newImage;
  190. }
  191.  
  192.  
  193.  
  194.  
  195.  
  196. - (UIImage*)imageByScalingAndCroppingForSize:(UIImage*)anImage toSize:(CGSize)targetSize
  197. {
  198.     UIImage *sourceImage = anImage;
  199.     UIImage *newImage = nil;
  200.     CGSize imageSize = sourceImage.size;
  201.    
  202.    
  203.     CGFloat width = imageSize.width;
  204.     CGFloat height = imageSize.height;
  205.    
  206.     CGFloat targetWidth = targetSize.width;
  207.     CGFloat targetHeight = targetSize.height;
  208.     CGFloat scaleFactor = 0.0;
  209.     CGFloat scaledWidth = targetWidth;
  210.     CGFloat scaledHeight = targetHeight;
  211.     CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
  212.    
  213.    
  214.    
  215.     if (CGSizeEqualToSize(imageSize, targetSize) == NO)
  216.     {
  217.         CGFloat widthFactor = targetWidth / width;
  218.         CGFloat heightFactor = targetHeight / height;
  219.        
  220.         if (widthFactor > heightFactor)
  221.             scaleFactor = widthFactor; // scale to fit height
  222.         else
  223.             scaleFactor = heightFactor; // scale to fit width
  224.         scaledWidth = width * scaleFactor;
  225.         scaledHeight = height * scaleFactor;
  226.        
  227.         // center the image
  228.         if (widthFactor > heightFactor)
  229.         {
  230.             thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
  231.         }
  232.         else
  233.             if (widthFactor < heightFactor)
  234.             {
  235.                 thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
  236.             }
  237.     }
  238.    
  239.     UIGraphicsBeginImageContext(targetSize); // this will crop
  240.    
  241.     CGRect thumbnailRect = CGRectZero;
  242.     thumbnailRect.origin = thumbnailPoint;
  243.     thumbnailRect.size.width = scaledWidth;
  244.     thumbnailRect.size.height = scaledHeight;
  245.    
  246.     [sourceImage drawInRect:thumbnailRect];
  247.    
  248.     newImage = UIGraphicsGetImageFromCurrentImageContext();
  249.     if(newImage == nil)
  250.         NSLog(@"could not scale image");
  251.    
  252.     //pop the context to get back to the default
  253.     UIGraphicsEndImageContext();
  254.     return newImage;
  255. }
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement