Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Phonegap Prompt Plugin
- // Copyright (c) Paul Panserrieu, Zenexity 2011
- // MIT Licensed
- #import "imagePicker.h"
- @implementation imagePicker
- - (void) show:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
- AGImagePickerController *imagePickerController = [[AGImagePickerController alloc] initWithFailureBlock:^(NSError *error) {
- if (error == nil)
- {
- NSLog(@"User has cancelled.");
- [[self viewController] dismissModalViewControllerAnimated:YES];
- } else
- {
- NSLog(@"Error: %@", error);
- // Wait for the view controller to show first and hide it after that
- double delayInSeconds = 0.5;
- dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
- dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
- [[self viewController] dismissModalViewControllerAnimated:YES];
- });
- }
- [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
- } andSuccessBlock:^(NSArray *info) {
- NSUInteger count = [info count];
- int i = 0;
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
- NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"captured_photos"];
- for (i = 0; i < count; i++) {
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"yyMMddHHmmss"];
- NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
- [dateFormatter release];
- int randomValue = arc4random() % 1000;
- NSString *uniqueStr = [NSString stringWithFormat:@"%@.%d",dateString,randomValue];
- CLLocation * loc = [[info objectAtIndex: i] valueForProperty:ALAssetPropertyLocation];
- if (loc == nil)
- {
- // continue;
- }
- NSDate * pNSDate = [[info objectAtIndex: i] valueForProperty:ALAssetPropertyDate];
- ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation];
- UIImage * image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
- NSString * orientation;
- switch ([rep orientation]) {
- case UIImageOrientationUp:
- orientation = @"up";
- break;
- case UIImageOrientationDown:
- orientation = @"down";
- image = rotateUIImage(image, 180);
- break;
- case UIImageOrientationRight:
- orientation = @"right";
- image = rotateUIImage(image, 90);
- break;
- case UIImageOrientationLeft:
- orientation = @"left";
- image = rotateUIImage(image, -90);
- break;
- default:
- orientation = @"default";
- break;
- }
- NSInteger width = image.size.width;
- NSInteger height = image.size.height;
- NSLog(@" image.size.width %f -- image.size.height %f " , image.size.width , image.size.height);
- if (image.size.width > 612)
- {
- image = [self imageByScalingAndCroppingForSize:image toSize:CGSizeMake(612,612*image.size.height/image.size.width)];
- NSLog(@" croping to :: 612*%f " , 612*image.size.height/image.size.width);
- }
- NSString * imageFileName = [@"/_" stringByAppendingFormat:@"%@_w_%d_h_%f_o_%@.jpg" ,uniqueStr , 612 , 612*image.size.height/image.size.width , orientation];
- NSString* filePath =[dataPath stringByAppendingFormat:imageFileName];
- NSLog(@" pNSDate %@" , pNSDate);
- NSString * timestamp = [NSString stringWithFormat:@"%.0f", [pNSDate timeIntervalSince1970]];
- double timestampDB = [timestamp doubleValue];
- int timestampint = (int)(timestampDB + (timestampDB>0 ? 0.5 : -0.5));
- NSDate * nowNSDate = [NSDate date];
- NSString * nowtimestampStr = [NSString stringWithFormat:@"%.0f", [nowNSDate timeIntervalSince1970]];
- double nowtimestampDB = [nowtimestampStr doubleValue];
- int nowtimestampint = (int)(nowtimestampDB + (nowtimestampDB>0 ? 0.5 : -0.5));
- NSData *dataImageForLenght = UIImageJPEGRepresentation(image , 100 / 100.0f);
- if([dataImageForLenght writeToFile:filePath atomically:YES])
- {
- NSString * jsCallBack = [NSString stringWithFormat:
- @"signal.apis.imageWasCaptured({path:'%@',mode:'cr',width:%d,height:%d,location:{timestamp: %.00f, \
- coords:{latitude:%f,longitude:%f,altitude:%.02f,heading:%.02f,speed:%.02f,accuracy:%.02f, altitudeAccuracy:%.02f} \
- }, createdSince:'%d' , orientation:'%@'})",
- imageFileName , width , height,
- [loc.timestamp timeIntervalSince1970] * 1000.0,
- loc.coordinate.latitude,
- loc.coordinate.longitude,
- loc.altitude,
- loc.course,
- loc.speed,
- loc.horizontalAccuracy,
- loc.verticalAccuracy,
- nowtimestampint-timestampint,
- orientation
- ];
- [self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
- }
- // NSString *const ALAssetPropertyType;
- // NSString *const ALAssetPropertyLocation;
- // NSString *const ALAssetPropertyDuration;
- // NSString *const ALAssetPropertyOrientation;
- // NSString *const ALAssetPropertyDate;
- // NSString *const ALAssetPropertyRepresentations;
- // NSString *const ALAssetPropertyURLs;
- }
- NSString * jsCallBack2 = [NSString stringWithFormat:@"signal.app.goToPage('capturedPhotos_page');"];
- [self.webView stringByEvaluatingJavaScriptFromString:jsCallBack2];
- [[self viewController] dismissModalViewControllerAnimated:YES];
- [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
- }];
- [[self viewController] presentModalViewController:imagePickerController animated:YES];
- [imagePickerController release];
- }
- UIImage* rotateUIImage(const UIImage* src, float angleDegrees) {
- UIView* rotatedViewBox = [[UIView alloc] initWithFrame: CGRectMake(0, 0, src.size.width, src.size.height)];
- float angleRadians = angleDegrees * ((float)M_PI / 180.0f);
- CGAffineTransform t = CGAffineTransformMakeRotation(angleRadians);
- rotatedViewBox.transform = t;
- CGSize rotatedSize = rotatedViewBox.frame.size;
- [rotatedViewBox release];
- UIGraphicsBeginImageContext(rotatedSize);
- CGContextRef bitmap = UIGraphicsGetCurrentContext();
- CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
- CGContextRotateCTM(bitmap, angleRadians);
- CGContextScaleCTM(bitmap, 1.0, -1.0);
- CGContextDrawImage(bitmap, CGRectMake(-src.size.width / 2, -src.size.height / 2, src.size.width, src.size.height), [src CGImage]);
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return newImage;
- }
- - (UIImage*)imageByScalingAndCroppingForSize:(UIImage*)anImage toSize:(CGSize)targetSize
- {
- UIImage *sourceImage = anImage;
- UIImage *newImage = nil;
- CGSize imageSize = sourceImage.size;
- CGFloat width = imageSize.width;
- CGFloat height = imageSize.height;
- CGFloat targetWidth = targetSize.width;
- CGFloat targetHeight = targetSize.height;
- CGFloat scaleFactor = 0.0;
- CGFloat scaledWidth = targetWidth;
- CGFloat scaledHeight = targetHeight;
- CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
- if (CGSizeEqualToSize(imageSize, targetSize) == NO)
- {
- CGFloat widthFactor = targetWidth / width;
- CGFloat heightFactor = targetHeight / height;
- if (widthFactor > heightFactor)
- scaleFactor = widthFactor; // scale to fit height
- else
- scaleFactor = heightFactor; // scale to fit width
- scaledWidth = width * scaleFactor;
- scaledHeight = height * scaleFactor;
- // center the image
- if (widthFactor > heightFactor)
- {
- thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
- }
- else
- if (widthFactor < heightFactor)
- {
- thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
- }
- }
- UIGraphicsBeginImageContext(targetSize); // this will crop
- CGRect thumbnailRect = CGRectZero;
- thumbnailRect.origin = thumbnailPoint;
- thumbnailRect.size.width = scaledWidth;
- thumbnailRect.size.height = scaledHeight;
- [sourceImage drawInRect:thumbnailRect];
- newImage = UIGraphicsGetImageFromCurrentImageContext();
- if(newImage == nil)
- NSLog(@"could not scale image");
- //pop the context to get back to the default
- UIGraphicsEndImageContext();
- return newImage;
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement