Skip to content

Commit 94c54f0

Browse files
committedSep 15, 2015
Update - 2.0.9
1 parent 4474290 commit 94c54f0

File tree

5 files changed

+60
-15
lines changed

5 files changed

+60
-15
lines changed
 

‎README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ cordova-obimagepicker
44
Cordova Plugin For Multiple Image Selection - implemented for iOS and Android 4.0 and above.
55
It can be used as a replacement for http://ngcordova.com/docs/plugins/imagePicker/
66

7+
* exports images in Base64 format
8+
79
## Installing the plugin
810

911
The plugin conforms to the Cordova plugin specification, it can be installed
@@ -40,7 +42,8 @@ window.imagePicker.getPictures(
4042
}, function (error) {
4143
console.log('Error: ' + error);
4244
}, {
43-
maximumImagesCount: 10
45+
maximumImagesCount: 10,
46+
width: 100 // Set with for image - will keep the aspect ratio
4447
}
4548
);
4649
```
@@ -50,6 +53,8 @@ window.imagePicker.getPictures(
5053
options = {
5154
// max images to be selected, defaults to 0 (infinity)
5255
maximumImagesCount: int,
56+
// set with of the new returned image - will keep the aspect ratio
57+
width: int
5358
};
5459

5560
### Note for Android Use

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cordova-obimagepicker",
3-
"version": "2.0.8",
3+
"version": "2.0.9",
44
"description": "Cordova file picker",
55
"main": "index.js",
66
"scripts": {

‎plugin.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
id="ro.qualitance.obimagepicker"
5-
version="2.0.8">
5+
version="2.0.9">
66

77
<name>ObImagePicker</name>
88

‎src/ios/SOSPicker.m

+23-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ - (void) getPictures:(CDVInvokedUrlCommand *)command {
2323

2424
NSDictionary *options = [command.arguments objectAtIndex: 0];
2525
NSInteger maximumImagesCount = [[options objectForKey:@"maximumImagesCount"] integerValue];
26+
self.width = [[options objectForKey:@"width"] integerValue];
2627

2728
QBImagePickerController *imagePickerController = [QBImagePickerController new];
2829
imagePickerController.delegate = self;
@@ -47,17 +48,33 @@ - (void)qb_imagePickerController:(QBImagePickerController *)imagePickerControlle
4748
NSMutableArray *resultStrings = [[NSMutableArray alloc] init];
4849
PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init];
4950
imageRequestOptions.synchronous = YES;
50-
51+
5152

5253
for (PHAsset *asset in assets) {
5354
// Do something with the asset
5455
if(asset.mediaType == PHAssetMediaTypeImage)
5556
{
56-
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:imageRequestOptions resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
57-
//NSString *encodedString = [imageData base64Encoding];
58-
59-
NSString *encodedString = [imageData base64EncodedStringWithOptions:0];
60-
[resultStrings addObject: encodedString];
57+
//[[PHImageManager defaultManager] requestImageDataForAsset:asset options:imageRequestOptions resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
58+
// NSString *encodedString = [imageData base64Encoding];
59+
// NSString *encodedString = [imageData base64EncodedStringWithOptions:0];
60+
// [resultStrings addObject: encodedString];
61+
//}];
62+
63+
float imageWidth = asset.pixelWidth;
64+
float imageHeight = asset.pixelHeight;
65+
CGSize newImageSize;
66+
67+
// Keep aspect ratio
68+
if(self.width){
69+
self.height = (width * imageHeight) / imageWidth;
70+
newImageSize = CGSizeMake(width, height);
71+
}
72+
else{
73+
newImageSize = CGSizeMake(imageWidth, imageHeight);
74+
}
75+
// Resize the image
76+
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:newImageSize contentMode:PHImageContentModeAspectFit options:imageRequestOptions resultHandler:^(UIImage *result, NSDictionary *info) {
77+
[resultStrings addObject:[self encodeToBase64String:result]];
6178
}];
6279
}
6380
}

‎test/imagepicker/imagepicker/ViewController.m

+29-6
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@ - (NSString *)encodeToBase64String:(UIImage *)image {
4646

4747
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets
4848
{
49+
4950
NSLog(@"Selected assets:");
5051
NSLog(@"%@", assets);
5152
NSMutableArray *resultStrings = [[NSMutableArray alloc] init];
5253

53-
PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init];
54+
PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc] init];
5455
imageRequestOptions.synchronous = YES;
55-
56+
57+
float width = 375;
58+
__block float height = 500;
5659

5760
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
5861
dispatch_async(queue, ^{
@@ -64,11 +67,31 @@ - (void)qb_imagePickerController:(QBImagePickerController *)imagePickerControlle
6467
// Do something with the asset
6568
if(asset.mediaType == PHAssetMediaTypeImage)
6669
{
67-
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:imageRequestOptions resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
70+
// [[PHImageManager defaultManager] requestImageDataForAsset:asset options:imageRequestOptions resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
71+
//
72+
// UIImage *image = [UIImage imageWithData:imageData];
73+
// // NSString *imgData = [self encodeToBase64String:image];
74+
// [resultStrings addObject:image];
75+
// }];
76+
77+
78+
float imageWidth = asset.pixelWidth;
79+
float imageHeight = asset.pixelHeight;
80+
CGSize newImageSize;
81+
82+
// Keep aspect ratio
83+
if(width){
84+
height = (width * imageHeight) / imageWidth;
85+
newImageSize = CGSizeMake(width, height);
86+
}
87+
else{
88+
newImageSize = CGSizeMake(imageWidth, imageHeight);
89+
}
90+
91+
// Resize the image
92+
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:newImageSize contentMode:PHImageContentModeAspectFit options:imageRequestOptions resultHandler:^(UIImage *result, NSDictionary *info) {
6893

69-
UIImage *image = [UIImage imageWithData:imageData];
70-
// NSString *imgData = [self encodeToBase64String:image];
71-
[resultStrings addObject:image];
94+
[resultStrings addObject:[self encodeToBase64String:result]];
7295
}];
7396
}
7497
}

0 commit comments

Comments
 (0)
Failed to load comments.