nativescript-imagepicker
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -1,2 +0,2 @@ | ||
declare module "imagepicker" { | ||
declare module "nativescript-imagepicker" { | ||
@@ -6,2 +6,15 @@ import observable = require("data/observable"); | ||
export interface ImageOptions { | ||
/** | ||
* The maximum width that the image is allowed to be. | ||
*/ | ||
maxWidth?: number; | ||
/** | ||
* The maximum height that the image is allowed to be. | ||
*/ | ||
maxHeight?: number; | ||
} | ||
export class SelectedAsset extends observable.Observable { | ||
@@ -30,3 +43,15 @@ /** | ||
/** | ||
* Asynchronously retrieves an ImageSource object that represents this selected image. | ||
* Scaled to the given size. (Aspect-ratio is preserved by default) | ||
*/ | ||
getImage(options?: ImageOptions): Promise<imagesource.ImageSource>; | ||
/** | ||
* Asynchronously retrieves an ArrayBuffer that represents the raw byte data from this selected image. | ||
*/ | ||
getImageData(): Promise<ArrayBuffer>; | ||
/** | ||
* For iOS Returns a promise with NSData representation of the asset. | ||
* On Android, Returns a promise with a java.io.InputStream. | ||
* Note that in future versions it should return ArrayBuffer. | ||
@@ -58,3 +83,3 @@ */ | ||
*/ | ||
selectionMode?: string; | ||
mode?: string; | ||
} | ||
@@ -61,0 +86,0 @@ |
@@ -8,18 +8,13 @@ { | ||
"name": "nativescript-imagepicker", | ||
"version": "0.0.4", | ||
"nativescript": { | ||
"id": "org.nativescript.imagepicker", | ||
"platforms": { | ||
"tns-android": { | ||
"version": "1.4.0" | ||
}, | ||
"tns-ios": { | ||
"version": "1.4.0" | ||
} | ||
} | ||
}, | ||
"version": "0.0.5", | ||
"nativescript": { | ||
"platforms": { | ||
"android": "2.0.0", | ||
"ios": "2.0.0" | ||
} | ||
}, | ||
"dependencies": { | ||
"tns-core-modules": "1.4.0" | ||
"tns-core-modules": "2.0.0" | ||
}, | ||
"main": "viewmodel.js" | ||
} |
@@ -13,2 +13,3 @@ var __extends = (this && this.__extends) || function (d, b) { | ||
var BitmapFactory = android.graphics.BitmapFactory; | ||
var StaticArrayBuffer = ArrayBuffer; | ||
var SelectedAsset = (function (_super) { | ||
@@ -24,2 +25,28 @@ __extends(SelectedAsset, _super); | ||
}; | ||
SelectedAsset.prototype.getImage = function (options) { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
try { | ||
resolve(_this.decodeUri(_this._uri, options)); | ||
} | ||
catch (ex) { | ||
reject(ex); | ||
} | ||
}); | ||
}; | ||
SelectedAsset.prototype.getImageData = function () { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
try { | ||
if (!_this._data) { | ||
var bb = _this.getByteBuffer(_this._uri); | ||
_this._data = StaticArrayBuffer.from(bb); | ||
} | ||
resolve(_this._data); | ||
} | ||
catch (ex) { | ||
reject(ex); | ||
} | ||
}); | ||
}; | ||
Object.defineProperty(SelectedAsset.prototype, "thumb", { | ||
@@ -92,25 +119,59 @@ get: function () { | ||
SelectedAsset.prototype.decodeThumbUri = function () { | ||
var REQUIRED_SIZE = { | ||
maxWidth: 100, | ||
maxHeight: 100 | ||
}; | ||
this._thumb = this.decodeUri(this._uri, REQUIRED_SIZE); | ||
this.notifyPropertyChange("thumb", this._thumb); | ||
}; | ||
SelectedAsset.prototype.getSampleSize = function (uri, options) { | ||
var boundsOptions = new BitmapFactory.Options(); | ||
boundsOptions.inJustDecodeBounds = true; | ||
BitmapFactory.decodeStream(this.getContentResolver().openInputStream(this._uri), null, boundsOptions); | ||
var REQUIRED_SIZE = 100; | ||
BitmapFactory.decodeStream(this.openInputStream(uri), null, boundsOptions); | ||
var outWidth = boundsOptions.outWidth; | ||
var outHeight = boundsOptions.outHeight; | ||
var scale = 1; | ||
while (true) { | ||
if (outWidth / 2 < REQUIRED_SIZE | ||
|| outHeight / 2 < REQUIRED_SIZE) { | ||
break; | ||
if (options) { | ||
var targetSize = options.maxWidth < options.maxHeight ? options.maxWidth : options.maxHeight; | ||
while (!(this.matchesSize(targetSize, outWidth) || | ||
this.matchesSize(targetSize, outHeight))) { | ||
outWidth /= 2; | ||
outHeight /= 2; | ||
scale *= 2; | ||
} | ||
outWidth /= 2; | ||
outHeight /= 2; | ||
scale *= 2; | ||
} | ||
return scale; | ||
}; | ||
SelectedAsset.prototype.matchesSize = function (targetSize, actualSize) { | ||
return targetSize && actualSize / 2 < targetSize; | ||
}; | ||
SelectedAsset.prototype.decodeUri = function (uri, options) { | ||
var downsampleOptions = new BitmapFactory.Options(); | ||
downsampleOptions.inSampleSize = scale; | ||
var bitmap = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(this._uri), null, downsampleOptions); | ||
this._thumb = new imagesource.ImageSource(); | ||
this._thumb.setNativeSource(bitmap); | ||
this.notifyPropertyChange("thumb", this._thumb); | ||
downsampleOptions.inSampleSize = this.getSampleSize(uri, options); | ||
var bitmap = BitmapFactory.decodeStream(this.openInputStream(uri), null, downsampleOptions); | ||
var image = new imagesource.ImageSource(); | ||
image.setNativeSource(bitmap); | ||
return image; | ||
}; | ||
SelectedAsset.prototype.getByteBuffer = function (uri) { | ||
var file = null; | ||
try { | ||
file = this.getContentResolver().openAssetFileDescriptor(uri, "r"); | ||
var length = file.getLength(); | ||
var buffer = java.nio.ByteBuffer.allocateDirect(length); | ||
var bytes = buffer.array(); | ||
var stream = file.createInputStream(); | ||
var reader = new java.io.BufferedInputStream(stream, 4096); | ||
reader.read(bytes, 0, bytes.length); | ||
return buffer; | ||
} | ||
finally { | ||
if (file) { | ||
file.close(); | ||
} | ||
} | ||
}; | ||
SelectedAsset.prototype.openInputStream = function (uri) { | ||
return this.getContentResolver().openInputStream(uri); | ||
}; | ||
SelectedAsset.prototype.getContentResolver = function () { | ||
@@ -117,0 +178,0 @@ return application.android.nativeApp.getContentResolver(); |
@@ -173,2 +173,8 @@ var __extends = (this && this.__extends) || function (d, b) { | ||
}); | ||
SelectedAsset.prototype.getImage = function (options) { | ||
return Promise.reject(new Error("getImage() is not implemented in SelectedAsset. Derived classes should implement it to be fully functional.")); | ||
}; | ||
SelectedAsset.prototype.getImageData = function () { | ||
return Promise.reject(new Error("getImageData() is not implemented in SelectedAsset. Derived classes should implement it to be fully functional.")); | ||
}; | ||
return SelectedAsset; | ||
@@ -182,2 +188,3 @@ })(data_observable.Observable); | ||
this._album = album; | ||
this._image = null; | ||
} | ||
@@ -304,2 +311,22 @@ Object.defineProperty(Asset.prototype, "album", { | ||
}; | ||
ImagePickerPH.prototype.createPHImage = function (image, options) { | ||
return new Promise(function (resolve, reject) { | ||
var size = options ? CGSizeMake(options.maxWidth, options.maxHeight) : PHImageManagerMaximumSize; | ||
var resizeMode = PHImageRequestOptions.alloc().init(); | ||
resizeMode.resizeMode = PHImageRequestOptionsResizeMode.PHImageRequestOptionsResizeModeExact; | ||
resizeMode.synchronous = false; | ||
resizeMode.deliveryMode = PHImageRequestOptionsDeliveryMode.PHImageRequestOptionsDeliveryModeHighQualityFormat; | ||
resizeMode.normalizedCropRect = CGRectMake(0, 0, 1, 1); | ||
PHImageManager.defaultManager().requestImageForAssetTargetSizeContentModeOptionsResultHandler(image, size, PHImageContentMode.PHImageContentModeAspectFill, resizeMode, function (createdImage, data) { | ||
if (createdImage) { | ||
var imageSource = new image_source.ImageSource(); | ||
imageSource.setNativeSource(createdImage); | ||
resolve(imageSource); | ||
} | ||
else { | ||
reject(new Error("The image could not be created.")); | ||
} | ||
}); | ||
}); | ||
}; | ||
ImagePickerPH.prototype.done = function () { | ||
@@ -368,2 +395,10 @@ var result = []; | ||
}); | ||
AssetPH.prototype.getImage = function (options) { | ||
return this.album.imagePicker.createPHImage(this._phAsset, options); | ||
}; | ||
AssetPH.prototype.getImageData = function () { | ||
return this.data().then(function (data) { | ||
return interop.bufferFromData(data); | ||
}); | ||
}; | ||
Object.defineProperty(AssetPH.prototype, "fileUri", { | ||
@@ -370,0 +405,0 @@ get: function () { |
34822
830
+ Addedtns-core-modules@2.0.0(transitive)
+ Addedtns-core-modules-widgets@2.0.0(transitive)
- Removedtns-core-modules@1.4.0(transitive)
Updatedtns-core-modules@2.0.0