diff --git a/src/ios/SOSPicker.h b/src/ios/SOSPicker.h index 71081ec..49594e3 100644 --- a/src/ios/SOSPicker.h +++ b/src/ios/SOSPicker.h @@ -7,10 +7,9 @@ // #import -#import "ELCAlbumPickerController.h" -#import "ELCImagePickerController.h" -@interface SOSPicker : CDVPlugin + +@interface SOSPicker : CDVPlugin < UINavigationControllerDelegate, UIScrollViewDelegate> @property (copy) NSString* callbackId; diff --git a/src/ios/SOSPicker.m b/src/ios/SOSPicker.m index da08db2..106d5ea 100644 --- a/src/ios/SOSPicker.m +++ b/src/ios/SOSPicker.m @@ -7,157 +7,123 @@ // #import "SOSPicker.h" -#import "ELCAlbumPickerController.h" -#import "ELCImagePickerController.h" -#import "ELCAssetTablePicker.h" + + +#import "GMImagePickerController.h" +#import "GMFetchItem.h" #define CDV_PHOTO_PREFIX @"cdv_photo_" -@implementation SOSPicker + +@interface SOSPicker () +@end + +@implementation SOSPicker @synthesize callbackId; - (void) getPictures:(CDVInvokedUrlCommand *)command { - NSDictionary *options = [command.arguments objectAtIndex: 0]; - - NSInteger maximumImagesCount = [[options objectForKey:@"maximumImagesCount"] integerValue]; - self.width = [[options objectForKey:@"width"] integerValue]; - self.height = [[options objectForKey:@"height"] integerValue]; - self.quality = [[options objectForKey:@"quality"] integerValue]; - - // Create the an album controller and image picker - ELCAlbumPickerController *albumController = [[ELCAlbumPickerController alloc] init]; - - if (maximumImagesCount == 1) { - albumController.immediateReturn = true; - albumController.singleSelection = true; - } else { - albumController.immediateReturn = false; - albumController.singleSelection = false; - } - - ELCImagePickerController *imagePicker = [[ELCImagePickerController alloc] initWithRootViewController:albumController]; - imagePicker.maximumImagesCount = maximumImagesCount; - imagePicker.returnsOriginalImage = 1; - imagePicker.imagePickerDelegate = self; - - albumController.parent = imagePicker; - self.callbackId = command.callbackId; - // Present modally - [self.viewController presentViewController:imagePicker - animated:YES - completion:nil]; + + NSArray * args = [ command arguments ]; + + BOOL allow_video = [ [ args[0] objectForKey:@"allow_video" ] boolValue ]; + NSString * title = [args[0] objectForKey:@"title"]; + NSString * message = [args[0] objectForKey:@"message"]; + + self.callbackId = command.callbackId; + [self launchGMImagePicker:allow_video title:title message:message]; } - -- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info { - CDVPluginResult* result = nil; - NSMutableArray *resultStrings = [[NSMutableArray alloc] init]; - NSData* data = nil; - NSString* docsPath = [NSTemporaryDirectory()stringByStandardizingPath]; - NSError* err = nil; - NSFileManager* fileMgr = [[NSFileManager alloc] init]; - NSString* filePath; - ALAsset* asset = nil; - UIImageOrientation orientation = UIImageOrientationUp;; - CGSize targetSize = CGSizeMake(self.width, self.height); - for (NSDictionary *dict in info) { - asset = [dict objectForKey:@"ALAsset"]; - // From ELCImagePickerController.m - - int i = 1; - do { - filePath = [NSString stringWithFormat:@"%@/%@%03d.%@", docsPath, CDV_PHOTO_PREFIX, i++, @"jpg"]; - } while ([fileMgr fileExistsAtPath:filePath]); - - @autoreleasepool { - ALAssetRepresentation *assetRep = [asset defaultRepresentation]; - CGImageRef imgRef = NULL; - - //defaultRepresentation returns image as it appears in photo picker, rotated and sized, - //so use UIImageOrientationUp when creating our image below. - if (picker.returnsOriginalImage) { - imgRef = [assetRep fullResolutionImage]; - orientation = [assetRep orientation]; - } else { - imgRef = [assetRep fullScreenImage]; - } - - UIImage* image = [UIImage imageWithCGImage:imgRef scale:1.0f orientation:orientation]; - if (self.width == 0 && self.height == 0) { - data = UIImageJPEGRepresentation(image, self.quality/100.0f); - } else { - UIImage* scaledImage = [self imageByScalingNotCroppingForSize:image toSize:targetSize]; - data = UIImageJPEGRepresentation(scaledImage, self.quality/100.0f); - } - - if (![data writeToFile:filePath options:NSAtomicWrite error:&err]) { - result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsString:[err localizedDescription]]; - break; - } else { - [resultStrings addObject:[[NSURL fileURLWithPath:filePath] absoluteString]]; - } - } - - } - - if (nil == result) { - result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:resultStrings]; - } - - [self.viewController dismissViewControllerAnimated:YES completion:nil]; - [self.commandDelegate sendPluginResult:result callbackId:self.callbackId]; +- (void)launchGMImagePicker:(bool)allow_video title:(NSString *)title message:(NSString *)message +{ + GMImagePickerController *picker = [[GMImagePickerController alloc] init:allow_video]; + picker.delegate = self; + picker.title = title; + picker.customNavigationBarPrompt = message; + picker.colsInPortrait = 4; + picker.colsInLandscape = 6; + picker.minimumInteritemSpacing = 2.0; + picker.modalPresentationStyle = UIModalPresentationPopover; + + UIPopoverPresentationController *popPC = picker.popoverPresentationController; + popPC.permittedArrowDirections = UIPopoverArrowDirectionAny; + popPC.sourceView = picker.view; + //popPC.sourceRect = nil; + + [self.viewController showViewController:picker sender:nil]; } -- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker { - [self.viewController dismissViewControllerAnimated:YES completion:nil]; - CDVPluginResult* pluginResult = nil; - NSArray* emptyArray = [NSArray array]; - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:emptyArray]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId]; -} - (UIImage*)imageByScalingNotCroppingForSize:(UIImage*)anImage toSize:(CGSize)frameSize { - UIImage* sourceImage = anImage; - UIImage* newImage = nil; - CGSize imageSize = sourceImage.size; - CGFloat width = imageSize.width; - CGFloat height = imageSize.height; - CGFloat targetWidth = frameSize.width; - CGFloat targetHeight = frameSize.height; - CGFloat scaleFactor = 0.0; - CGSize scaledSize = frameSize; - - if (CGSizeEqualToSize(imageSize, frameSize) == NO) { - CGFloat widthFactor = targetWidth / width; - CGFloat heightFactor = targetHeight / height; - - // opposite comparison to imageByScalingAndCroppingForSize in order to contain the image within the given bounds - if (widthFactor == 0.0) { - scaleFactor = heightFactor; - } else if (heightFactor == 0.0) { - scaleFactor = widthFactor; - } else if (widthFactor > heightFactor) { - scaleFactor = heightFactor; // scale to fit height - } else { - scaleFactor = widthFactor; // scale to fit width - } - scaledSize = CGSizeMake(width * scaleFactor, height * scaleFactor); - } - - UIGraphicsBeginImageContext(scaledSize); // this will resize - - [sourceImage drawInRect:CGRectMake(0, 0, scaledSize.width, scaledSize.height)]; - - newImage = UIGraphicsGetImageFromCurrentImageContext(); - if (newImage == nil) { - NSLog(@"could not scale image"); - } - - // pop the context to get back to the default - UIGraphicsEndImageContext(); - return newImage; + return nil; } + +#pragma mark - UIImagePickerControllerDelegate + + +- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info +{ + + [picker.presentingViewController dismissViewControllerAnimated:YES completion:nil]; + NSLog(@"UIImagePickerController: User ended picking assets"); + + +} + +- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker +{ + [picker.presentingViewController dismissViewControllerAnimated:YES completion:nil]; + NSLog(@"UIImagePickerController: User pressed cancel button"); +} + +#pragma mark - GMImagePickerControllerDelegate + +- (void)assetsPickerController:(GMImagePickerController *)picker didFinishPickingAssets:(NSArray *)fetchArray +{ + [picker.presentingViewController dismissViewControllerAnimated:YES completion:nil]; + + NSLog(@"GMImagePicker: User ended picking assets. Number of selected items is: %lu", (unsigned long)fetchArray.count); + + CDVPluginResult* result = nil; + //NSMutableDictionary * result_all = [[NSMutableDictionary alloc] init]; + + NSMutableArray * result_all = [[NSMutableArray alloc] init]; + + for (GMFetchItem *item in fetchArray) { + + if ( !item.image_fullsize || !item.image_thumb ) { + continue; + } + + NSMutableDictionary * result_item = [[NSMutableDictionary alloc] init]; + + [ result_item setValue:item.image_fullsize forKey:@"original" ]; + [ result_item setValue:item.image_thumb forKey:@"thumb" ]; + + [ result_all addObject:result_item ]; + } + + //[ result_all setObject:result_fullsize forKey:@"actual" ]; + //[ result_all setObject:result_thumbnail forKey:@"thumb" ]; + + result = nil; + if (nil == result) { + //result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:resultStrings]; + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:result_all]; + } + + [self.viewController dismissViewControllerAnimated:YES completion:nil]; + [self.commandDelegate sendPluginResult:result callbackId:self.callbackId]; + +} + +//Optional implementation: +-(void)assetsPickerControllerDidCancel:(GMImagePickerController *)picker +{ + NSLog(@"GMImagePicker: User pressed cancel button"); +} + + @end