nativescript-imagepicker
Advanced tools
Comparing version 5.0.2 to 6.0.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var imagesource = require("tns-core-modules/image-source"); | ||
var application = require("tns-core-modules/application"); | ||
var imageAssetModule = require("tns-core-modules/image-asset"); | ||
var permissions = require("nativescript-permissions"); | ||
var SelectedAsset = (function (_super) { | ||
__extends(SelectedAsset, _super); | ||
function SelectedAsset(uri) { | ||
var _this = _super.call(this, SelectedAsset._calculateFileUri(uri)) || this; | ||
_this._uri = uri; | ||
return _this; | ||
var UriHelper = (function () { | ||
function UriHelper() { | ||
} | ||
SelectedAsset.prototype.data = function () { | ||
return Promise.reject(new Error("Not implemented.")); | ||
}; | ||
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 = ArrayBuffer.from(bb); | ||
} | ||
resolve(_this._data); | ||
} | ||
catch (ex) { | ||
reject(ex); | ||
} | ||
}); | ||
}; | ||
Object.defineProperty(SelectedAsset.prototype, "thumbAsset", { | ||
get: function () { | ||
return this._thumbAsset; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
SelectedAsset.prototype.setThumbAsset = function (value) { | ||
this._thumbAsset = value; | ||
this.notifyPropertyChange("thumbAsset", value); | ||
}; | ||
Object.defineProperty(SelectedAsset.prototype, "uri", { | ||
get: function () { | ||
return this._uri.toString(); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(SelectedAsset.prototype, "fileUri", { | ||
get: function () { | ||
if (!this._fileUri) { | ||
this._fileUri = SelectedAsset._calculateFileUri(this._uri); | ||
} | ||
return this._fileUri; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
SelectedAsset._calculateFileUri = function (uri) { | ||
UriHelper._calculateFileUri = function (uri) { | ||
var DocumentsContract = android.provider.DocumentsContract; | ||
@@ -77,3 +15,3 @@ var isKitKat = android.os.Build.VERSION.SDK_INT >= 19; | ||
var contentUri = null; | ||
if (SelectedAsset.isExternalStorageDocument(uri)) { | ||
if (UriHelper.isExternalStorageDocument(uri)) { | ||
docId = DocumentsContract.getDocumentId(uri); | ||
@@ -86,8 +24,8 @@ id = docId.split(":")[1]; | ||
} | ||
else if (SelectedAsset.isDownloadsDocument(uri)) { | ||
else if (UriHelper.isDownloadsDocument(uri)) { | ||
id = DocumentsContract.getDocumentId(uri); | ||
contentUri = android.content.ContentUris.withAppendedId(android.net.Uri.parse("content://downloads/public_downloads"), long(id)); | ||
return SelectedAsset.getDataColumn(contentUri, null, null); | ||
return UriHelper.getDataColumn(contentUri, null, null); | ||
} | ||
else if (SelectedAsset.isMediaDocument(uri)) { | ||
else if (UriHelper.isMediaDocument(uri)) { | ||
docId = DocumentsContract.getDocumentId(uri); | ||
@@ -108,3 +46,3 @@ var split = docId.split(":"); | ||
var selectionArgs = [id]; | ||
return SelectedAsset.getDataColumn(contentUri, selection, selectionArgs); | ||
return UriHelper.getDataColumn(contentUri, selection, selectionArgs); | ||
} | ||
@@ -114,3 +52,3 @@ } | ||
if ("content" === uri.getScheme()) { | ||
return SelectedAsset.getDataColumn(uri, null, null); | ||
return UriHelper.getDataColumn(uri, null, null); | ||
} | ||
@@ -123,3 +61,3 @@ else if ("file" === uri.getScheme()) { | ||
}; | ||
SelectedAsset.getDataColumn = function (uri, selection, selectionArgs) { | ||
UriHelper.getDataColumn = function (uri, selection, selectionArgs) { | ||
var cursor = null; | ||
@@ -148,85 +86,16 @@ var columns = [android.provider.MediaStore.MediaColumns.DATA]; | ||
}; | ||
SelectedAsset.isExternalStorageDocument = function (uri) { | ||
UriHelper.isExternalStorageDocument = function (uri) { | ||
return "com.android.externalstorage.documents" === uri.getAuthority(); | ||
}; | ||
SelectedAsset.isDownloadsDocument = function (uri) { | ||
UriHelper.isDownloadsDocument = function (uri) { | ||
return "com.android.providers.downloads.documents" === uri.getAuthority(); | ||
}; | ||
SelectedAsset.isMediaDocument = function (uri) { | ||
UriHelper.isMediaDocument = function (uri) { | ||
return "com.android.providers.media.documents" === uri.getAuthority(); | ||
}; | ||
SelectedAsset.prototype.decodeThumbAssetUri = function () { | ||
var REQUIRED_SIZE = { | ||
maxWidth: 100, | ||
maxHeight: 100 | ||
}; | ||
this._thumbAsset = this.decodeUriForImageAsset(this._uri, REQUIRED_SIZE); | ||
this.notifyPropertyChange("thumbAsset", this._thumbAsset); | ||
}; | ||
SelectedAsset.prototype.getSampleSize = function (uri, options) { | ||
var boundsOptions = new android.graphics.BitmapFactory.Options(); | ||
boundsOptions.inJustDecodeBounds = true; | ||
android.graphics.BitmapFactory.decodeStream(this.openInputStream(uri), null, boundsOptions); | ||
var outWidth = boundsOptions.outWidth; | ||
var outHeight = boundsOptions.outHeight; | ||
var scale = 1; | ||
if (options) { | ||
var targetSize = !options.maxWidth && options.maxHeight ? options.maxHeight : | ||
(!options.maxHeight && options.maxWidth ? options.maxWidth : | ||
(options.maxWidth < options.maxHeight ? options.maxWidth : options.maxHeight)); | ||
if (targetSize) { | ||
while (!(this.matchesSize(targetSize, outWidth) || | ||
this.matchesSize(targetSize, outHeight))) { | ||
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 android.graphics.BitmapFactory.Options(); | ||
downsampleOptions.inSampleSize = this.getSampleSize(uri, options); | ||
var bitmap = android.graphics.BitmapFactory.decodeStream(this.openInputStream(uri), null, downsampleOptions); | ||
var image = new imagesource.ImageSource(); | ||
image.setNativeSource(bitmap); | ||
return image; | ||
}; | ||
SelectedAsset.prototype.decodeUriForImageAsset = function (uri, options) { | ||
var downsampleOptions = new android.graphics.BitmapFactory.Options(); | ||
downsampleOptions.inSampleSize = this.getSampleSize(uri, options); | ||
var bitmap = android.graphics.BitmapFactory.decodeStream(this.openInputStream(uri), null, downsampleOptions); | ||
return new imageAssetModule.ImageAsset(bitmap); | ||
}; | ||
SelectedAsset.prototype.getByteBuffer = function (uri) { | ||
var file = null; | ||
try { | ||
file = SelectedAsset.getContentResolver().openAssetFileDescriptor(uri, "r"); | ||
var length_1 = file.getLength(); | ||
var buffer = java.nio.ByteBuffer.allocateDirect(length_1); | ||
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 SelectedAsset.getContentResolver().openInputStream(uri); | ||
}; | ||
SelectedAsset.getContentResolver = function () { | ||
UriHelper.getContentResolver = function () { | ||
return application.android.nativeApp.getContentResolver(); | ||
}; | ||
return SelectedAsset; | ||
}(imageAssetModule.ImageAsset)); | ||
exports.SelectedAsset = SelectedAsset; | ||
return UriHelper; | ||
}()); | ||
var ImagePicker = (function () { | ||
@@ -273,3 +142,4 @@ function ImagePicker(options) { | ||
if (uri) { | ||
results.push(new SelectedAsset(uri)); | ||
var selectedAsset = new imageAssetModule.ImageAsset(UriHelper._calculateFileUri(uri)); | ||
results.push(selectedAsset); | ||
} | ||
@@ -281,3 +151,4 @@ } | ||
var uri = data.getData(); | ||
results.push(new SelectedAsset(uri)); | ||
var selectedAsset = new imageAssetModule.ImageAsset(UriHelper._calculateFileUri(uri)); | ||
results.push(selectedAsset); | ||
} | ||
@@ -284,0 +155,0 @@ application.android.off(application.AndroidApplication.activityResultEvent, onResult); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var data_observable = require("tns-core-modules/data/observable"); | ||
var data_observablearray = require("tns-core-modules/data/observable-array"); | ||
var frame = require("tns-core-modules/ui/frame"); | ||
var imageAssetModule = require("tns-core-modules/image-asset"); | ||
var image_source = require("tns-core-modules/image-source"); | ||
var albumsModule; | ||
if (global.TNS_WEBPACK) { | ||
albumsModule = require("./albums.ios"); | ||
require("bundle-entry-points"); | ||
} | ||
else { | ||
albumsModule = require("./albums"); | ||
} | ||
var IMAGE_WIDTH = 80; | ||
var IMAGE_HEIGHT = 80; | ||
function create(options) { | ||
return new ImagePickerPH(options); | ||
} | ||
exports.create = create; | ||
var defaultAssetCollectionSubtypes = NSArray.arrayWithArray([ | ||
206, | ||
209, | ||
100, | ||
203, | ||
201, | ||
207, | ||
101, | ||
210, | ||
211, | ||
213 | ||
]); | ||
var ImagePicker = (function (_super) { | ||
__extends(ImagePicker, _super); | ||
function ImagePicker(options) { | ||
function ImagePicker(options, hostView) { | ||
if (options === void 0) { options = {}; } | ||
var _this = _super.call(this) || this; | ||
_this._selection = new data_observablearray.ObservableArray(); | ||
_this._albums = new data_observablearray.ObservableArray(); | ||
_this._options = options; | ||
_this._hostView = hostView; | ||
_this._imagePickerControllerDelegate = new ImagePickerControllerDelegate(); | ||
var imagePickerController = QBImagePickerController.alloc().init(); | ||
imagePickerController.assetCollectionSubtypes = defaultAssetCollectionSubtypes; | ||
imagePickerController.mediaType = options.mediaType ? options.mediaType.valueOf() : 0; | ||
imagePickerController.delegate = _this._imagePickerControllerDelegate; | ||
imagePickerController.allowsMultipleSelection = options.mode === 'multiple'; | ||
imagePickerController.minimumNumberOfSelection = options.minimumNumberOfSelection || 0; | ||
imagePickerController.maximumNumberOfSelection = options.maximumNumberOfSelection || 0; | ||
imagePickerController.showsNumberOfSelectedAssets = options.showsNumberOfSelectedAssets || true; | ||
imagePickerController.numberOfColumnsInPortrait = options.numberOfColumnsInPortrait || imagePickerController.numberOfColumnsInPortrait; | ||
imagePickerController.numberOfColumnsInLandscape = options.numberOfColumnsInLandscape || imagePickerController.numberOfColumnsInLandscape; | ||
imagePickerController.prompt = options.prompt || imagePickerController.prompt; | ||
_this._imagePickerController = imagePickerController; | ||
return _this; | ||
} | ||
ImagePicker.prototype.authorize = function () { | ||
return Promise.reject(new Error("Not implemented")); | ||
}; | ||
ImagePicker.prototype.present = function () { | ||
var _this = this; | ||
if (this._resolve || this._reject) { | ||
return Promise.reject(new Error("Selection is already in progress...")); | ||
} | ||
else { | ||
return new Promise(function (resolve, reject) { | ||
_this._resolve = resolve; | ||
_this._reject = reject; | ||
frame.topmost().navigate({ | ||
create: albumsModule.albumsPageFactory, | ||
context: _this | ||
}); | ||
}); | ||
} | ||
}; | ||
Object.defineProperty(ImagePicker.prototype, "albums", { | ||
Object.defineProperty(ImagePicker.prototype, "hostView", { | ||
get: function () { | ||
return this._albums; | ||
return this._hostView || frame.topmost(); | ||
}, | ||
@@ -57,205 +46,4 @@ enumerable: true, | ||
}); | ||
Object.defineProperty(ImagePicker.prototype, "selection", { | ||
get: function () { | ||
return this._selection; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(ImagePicker.prototype, "doneText", { | ||
get: function () { | ||
return this._options && this._options.doneText ? this._options.doneText : "Done"; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(ImagePicker.prototype, "cancelText", { | ||
get: function () { | ||
return this._options && this._options.cancelText ? this._options.cancelText : "Cancel"; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(ImagePicker.prototype, "albumsText", { | ||
get: function () { | ||
return this._options && this._options.albumsText ? this._options.albumsText : "Albums"; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(ImagePicker.prototype, "mode", { | ||
get: function () { | ||
return this._options && this._options.mode && this._options.mode.toLowerCase() === 'single' ? 'single' : 'multiple'; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(ImagePicker.prototype, "newestFirst", { | ||
get: function () { | ||
return this._options && !!this._options.newestFirst; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
ImagePicker.prototype.cancel = function () { | ||
this.notifyCanceled(); | ||
}; | ||
ImagePicker.prototype.done = function () { | ||
this.notifySelection([]); | ||
}; | ||
ImagePicker.prototype.notifyCanceled = function () { | ||
if (this._reject) { | ||
this._reject(new Error("Selection canceled.")); | ||
} | ||
}; | ||
ImagePicker.prototype.notifySelection = function (results) { | ||
if (this._resolve) { | ||
this._resolve(results); | ||
} | ||
}; | ||
return ImagePicker; | ||
}(data_observable.Observable)); | ||
exports.ImagePicker = ImagePicker; | ||
var Album = (function (_super) { | ||
__extends(Album, _super); | ||
function Album(imagePicker, title) { | ||
var _this = _super.call(this) || this; | ||
_this._imagePicker = imagePicker; | ||
_this._title = title; | ||
_this._assets = new data_observablearray.ObservableArray(); | ||
return _this; | ||
} | ||
Object.defineProperty(Album.prototype, "imagePicker", { | ||
get: function () { | ||
return this._imagePicker; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Album.prototype, "title", { | ||
get: function () { | ||
return this._title; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Album.prototype, "assets", { | ||
get: function () { | ||
return this._assets; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Album.prototype, "thumbAsset", { | ||
get: function () { | ||
return this._thumbAsset; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Album.prototype.setThumbAsset = function (value) { | ||
this._thumbAsset = value; | ||
this.notifyPropertyChange("thumbAsset", value); | ||
}; | ||
return Album; | ||
}(data_observable.Observable)); | ||
exports.Album = Album; | ||
var SelectedAsset = (function (_super) { | ||
__extends(SelectedAsset, _super); | ||
function SelectedAsset() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
Object.defineProperty(SelectedAsset.prototype, "uri", { | ||
get: function () { | ||
return null; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(SelectedAsset.prototype, "fileUri", { | ||
get: function () { | ||
return null; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
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; | ||
}(imageAssetModule.ImageAsset)); | ||
exports.SelectedAsset = SelectedAsset; | ||
var Asset = (function (_super) { | ||
__extends(Asset, _super); | ||
function Asset(album, asset) { | ||
var _this = _super.call(this, asset) || this; | ||
_this._album = album; | ||
_this._image = null; | ||
return _this; | ||
} | ||
Object.defineProperty(Asset.prototype, "album", { | ||
get: function () { | ||
return this._album; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Asset.prototype, "selected", { | ||
get: function () { | ||
return !!this._selected; | ||
}, | ||
set: function (value) { | ||
if (!!value === this.selected) | ||
return; | ||
var index = this.album.imagePicker.selection.indexOf(this); | ||
if (value) { | ||
this._selected = true; | ||
if (this.album.imagePicker.mode === "single") { | ||
if (this.album.imagePicker.selection.length > 0) { | ||
this.album.imagePicker.selection.getItem(0).selected = false; | ||
} | ||
} | ||
if (index < 0) { | ||
this.album.imagePicker.selection.push(this); | ||
} | ||
} | ||
else { | ||
delete this._selected; | ||
if (index >= 0) { | ||
this.album.imagePicker.selection.splice(index, 1); | ||
} | ||
} | ||
this.notifyPropertyChange("selected", this.selected); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Asset.prototype.toggleSelection = function (args) { | ||
this.selected = !this.selected; | ||
}; | ||
Asset.prototype.data = function () { | ||
return Promise.reject(new Error("Not implemented.")); | ||
}; | ||
return Asset; | ||
}(SelectedAsset)); | ||
exports.Asset = Asset; | ||
var ImagePickerPH = (function (_super) { | ||
__extends(ImagePickerPH, _super); | ||
function ImagePickerPH(options) { | ||
var _this = _super.call(this, options) || this; | ||
_this._thumbRequestOptions = PHImageRequestOptions.alloc().init(); | ||
_this._thumbRequestOptions.resizeMode = 2; | ||
_this._thumbRequestOptions.synchronous = false; | ||
_this._thumbRequestOptions.deliveryMode = 0; | ||
_this._thumbRequestOptions.networkAccessAllowed = true; | ||
_this._thumbRequestOptions.normalizedCropRect = CGRectMake(0, 0, 1, 1); | ||
_this._thumbRequestSize = CGSizeMake(80, 80); | ||
_this._options = options; | ||
_this._initialized = false; | ||
return _this; | ||
} | ||
ImagePickerPH.prototype.authorize = function () { | ||
ImagePicker.prototype.authorize = function () { | ||
console.log("authorizing..."); | ||
return new Promise(function (resolve, reject) { | ||
@@ -273,191 +61,43 @@ var runloop = CFRunLoopGetCurrent(); | ||
}; | ||
ImagePickerPH.prototype.present = function () { | ||
this.initialize(); | ||
return _super.prototype.present.call(this); | ||
}; | ||
ImagePickerPH.prototype.addAlbumsForFetchResult = function (result) { | ||
for (var i = 0; i < result.count; i++) { | ||
var item = result.objectAtIndex(i); | ||
if (item.isKindOfClass(PHAssetCollection)) { | ||
this.addAlbumForAssetCollection(item); | ||
} | ||
else { | ||
console.log("Ignored result: " + item); | ||
} | ||
} | ||
}; | ||
ImagePickerPH.prototype.addAlbumForAssetCollection = function (assetCollection) { | ||
var album = new AlbumPH(this, assetCollection.localizedTitle); | ||
var pfAssets = PHAsset.fetchAssetsInAssetCollectionOptions(assetCollection, null); | ||
album.addAssetsForFetchResult(pfAssets); | ||
if (album.assets.length > 0) { | ||
this.albums.push(album); | ||
} | ||
}; | ||
ImagePickerPH.prototype.createPHImageThumbAsset = function (target, asset) { | ||
PHImageManager.defaultManager().requestImageForAssetTargetSizeContentModeOptionsResultHandler(asset, this._thumbRequestSize, 1, this._thumbRequestOptions, function (target, uiImage, info) { | ||
var imageAsset = new imageAssetModule.ImageAsset(uiImage); | ||
imageAsset.options = { | ||
width: this._options.maxWidth && this._options.maxWidth < IMAGE_WIDTH ? this._options.maxWidth : IMAGE_WIDTH, | ||
height: this._options.maxHeight && this._options.IMAGE_HEIGHT < 80 ? this._options.IMAGE_HEIGHT : IMAGE_HEIGHT, | ||
keepAspectRatio: true | ||
}; | ||
target.setThumbAsset(imageAsset); | ||
}.bind(this, target)); | ||
}; | ||
ImagePickerPH.prototype.createPHImage = function (image, options) { | ||
ImagePicker.prototype.present = function () { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
var size = options ? CGSizeMake(options.maxWidth, options.maxHeight) : PHImageManagerMaximumSize; | ||
var resizeMode = PHImageRequestOptions.alloc().init(); | ||
var aspectRatio = (options && options.aspectRatio && options.aspectRatio === 'fill') ? 1 : 0; | ||
resizeMode.resizeMode = 2; | ||
resizeMode.synchronous = false; | ||
resizeMode.deliveryMode = 1; | ||
resizeMode.normalizedCropRect = CGRectMake(0, 0, 1, 1); | ||
PHImageManager.defaultManager().requestImageForAssetTargetSizeContentModeOptionsResultHandler(image, size, aspectRatio, 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.")); | ||
} | ||
}); | ||
_this._imagePickerControllerDelegate._resolve = resolve; | ||
_this.hostView.viewController.presentViewControllerAnimatedCompletion(_this._imagePickerController, true, null); | ||
}); | ||
}; | ||
ImagePickerPH.prototype.done = function () { | ||
var result = []; | ||
for (var i = 0; i < this.selection.length; ++i) { | ||
result.push(this.selection.getItem(i)); | ||
} | ||
this.notifySelection(result); | ||
return ImagePicker; | ||
}(data_observable.Observable)); | ||
exports.ImagePicker = ImagePicker; | ||
var ImagePickerControllerDelegate = (function (_super) { | ||
__extends(ImagePickerControllerDelegate, _super); | ||
function ImagePickerControllerDelegate() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
ImagePickerControllerDelegate.prototype.qb_imagePickerControllerDidCancel = function (imagePickerController) { | ||
imagePickerController.dismissViewControllerAnimatedCompletion(true, null); | ||
}; | ||
ImagePickerPH.prototype.initialize = function () { | ||
if (this._initialized) { | ||
return; | ||
} | ||
this._initialized = true; | ||
var smart = PHAssetCollection.fetchAssetCollectionsWithTypeSubtypeOptions(2, 2, null); | ||
this.addAlbumsForFetchResult(smart); | ||
var user = PHCollection.fetchTopLevelUserCollectionsWithOptions(null); | ||
this.addAlbumsForFetchResult(user); | ||
}; | ||
return ImagePickerPH; | ||
}(ImagePicker)); | ||
var AlbumPH = (function (_super) { | ||
__extends(AlbumPH, _super); | ||
function AlbumPH(imagePicker, title, options) { | ||
var _this = _super.call(this, imagePicker, title) || this; | ||
_this._setThumb = false; | ||
_this._options = options; | ||
return _this; | ||
} | ||
AlbumPH.prototype.addAssetsForFetchResult = function (result) { | ||
for (var i = 0; i < result.count; i++) { | ||
var asset = result.objectAtIndex(i); | ||
if (asset.isKindOfClass(PHAsset)) { | ||
this.addAsset(asset); | ||
ImagePickerControllerDelegate.prototype.qb_imagePickerControllerDidFinishPickingAssets = function (imagePickerController, iosAssets) { | ||
var assets = []; | ||
for (var i = 0; i < iosAssets.count; i++) { | ||
var asset = new imageAssetModule.ImageAsset(iosAssets[i]); | ||
if (!asset.options) { | ||
asset.options = { keepAspectRatio: true }; | ||
} | ||
else { | ||
console.log("Ignored asset: " + asset); | ||
} | ||
assets.push(asset); | ||
} | ||
this._resolve(assets); | ||
imagePickerController.dismissViewControllerAnimatedCompletion(true, null); | ||
}; | ||
AlbumPH.prototype.addAsset = function (asset) { | ||
var imagePicker = this.imagePicker; | ||
var item = new AssetPH(this, asset, this._options); | ||
if (!this._setThumb && imagePicker) { | ||
this._setThumb = true; | ||
imagePicker.createPHImageThumbAsset(this, asset); | ||
} | ||
if (this.imagePicker.newestFirst) { | ||
this.assets.unshift(item); | ||
} | ||
else { | ||
this.assets.push(item); | ||
} | ||
ImagePickerControllerDelegate.new = function () { | ||
return _super.new.call(this); | ||
}; | ||
return AlbumPH; | ||
}(Album)); | ||
var AssetPH = (function (_super) { | ||
__extends(AssetPH, _super); | ||
function AssetPH(album, phAsset, options) { | ||
var _this = _super.call(this, album, phAsset) || this; | ||
_this._phAsset = phAsset; | ||
_this._initializeOptions(options); | ||
return _this; | ||
} | ||
Object.defineProperty(AssetPH.prototype, "ios", { | ||
get: function () { | ||
return this._phAsset; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(AssetPH.prototype, "uri", { | ||
get: function () { | ||
return this._phAsset.localIdentifier.toString(); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
AssetPH.prototype._initializeOptions = function (options) { | ||
if (options) { | ||
this.options = { | ||
width: options.maxWidth && options.maxWidth < IMAGE_WIDTH ? options.maxWidth : IMAGE_WIDTH, | ||
height: options.maxHeight && options.maxHeight < IMAGE_HEIGHT ? options.maxHeight : IMAGE_HEIGHT, | ||
keepAspectRatio: true | ||
}; | ||
} | ||
else { | ||
this.options = { | ||
width: IMAGE_WIDTH, | ||
height: IMAGE_HEIGHT, | ||
keepAspectRatio: true | ||
}; | ||
} | ||
}; | ||
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", { | ||
get: function () { | ||
if (!AssetPH._uriRequestOptions) { | ||
AssetPH._uriRequestOptions = PHImageRequestOptions.alloc().init(); | ||
AssetPH._uriRequestOptions.synchronous = true; | ||
} | ||
var uri; | ||
PHImageManager.defaultManager().requestImageDataForAssetOptionsResultHandler(this._phAsset, AssetPH._uriRequestOptions, function (data, uti, orientation, info) { | ||
uri = info.objectForKey("PHImageFileURLKey"); | ||
}); | ||
if (uri) { | ||
return uri.toString(); | ||
} | ||
return undefined; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
AssetPH.prototype.data = function () { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
var runloop = CFRunLoopGetCurrent(); | ||
PHImageManager.defaultManager().requestImageDataForAssetOptionsResultHandler(_this._phAsset, null, function (data, dataUTI, orientation, info) { | ||
if (data) { | ||
resolve(data); | ||
} | ||
else { | ||
reject(new Error("Failed to get image data.")); | ||
} | ||
}); | ||
}); | ||
}; | ||
return AssetPH; | ||
}(Asset)); | ||
return ImagePickerControllerDelegate; | ||
}(NSObject)); | ||
ImagePickerControllerDelegate.ObjCProtocols = [QBImagePickerControllerDelegate]; | ||
exports.ImagePickerControllerDelegate = ImagePickerControllerDelegate; | ||
function create(options, hostView) { | ||
return new ImagePicker(options, hostView); | ||
} | ||
exports.create = create; | ||
//# sourceMappingURL=imagepicker.ios.js.map |
110
index.d.ts
import { Observable } from "tns-core-modules/data/observable"; | ||
import { ImageSource } from "tns-core-modules/image-source"; | ||
import { ImageAsset } from "tns-core-modules/image-asset"; | ||
import { View } from "tns-core-modules/ui/core/view/view"; | ||
export interface ImageOptions { | ||
export class ImagePicker { | ||
/** | ||
* The maximum width that the image is allowed to be. | ||
* Call this before 'present' to request any additional permissions that may be necessary. | ||
* In case of failed authorization consider notifying the user for degraded functionality. | ||
*/ | ||
maxWidth?: number; | ||
authorize(): Promise<void>; | ||
/** | ||
* The maximum height that the image is allowed to be. | ||
* Present the image picker UI. | ||
* The result will be an array of SelectedAsset instances provided when the promise is fulfilled. | ||
*/ | ||
maxHeight?: number; | ||
present(): Promise<ImageAsset[]>; | ||
} | ||
/** | ||
* iOS only. The image aspect ratio. Default value: fit. | ||
*/ | ||
aspectRatio?: "fill" | "fit"; | ||
export declare const enum ImagePickerMediaType { | ||
Any = 0, | ||
Image = 1, | ||
Video = 2 | ||
} | ||
export class SelectedAsset extends ImageAsset { | ||
/** | ||
* Provide options for the image picker. | ||
*/ | ||
interface Options { | ||
/** | ||
* A 100x100 pixels thumb of the selected image. | ||
* This property will be initialized on demand. The first access will return undefined or null. | ||
* It will trigger an async load and when the thumb is obtained, a property changed notification will occur. | ||
* Set the picker mode. Supported modes: "single" or "multiple" (default). | ||
*/ | ||
thumb: ImageSource; | ||
mode?: string; | ||
/** | ||
* URI that identifies the image asset. | ||
* Chances are you do not have permissions to read this uri. | ||
* The image data should be obtained using the other instance members. | ||
*/ | ||
uri: string; | ||
* Set the minumum number of selected assets in iOS | ||
*/ | ||
minimumNumberOfSelection?: number; | ||
/** | ||
* An URI that identifies the local asset file. | ||
* Chances are you do not have permissions to read this file. | ||
* The image data should be obtained using the other instance members. | ||
* Set the maximum number of selected assets in iOS | ||
*/ | ||
fileUri: string; | ||
maximumNumberOfSelection?: number; | ||
/** | ||
* Asynchronously retrieves an ImageSource object that represents this selected image. | ||
* Scaled to the given size. (Aspect-ratio is preserved by default) | ||
* Display the number of selected assets in iOS | ||
*/ | ||
getImage(options?: ImageOptions): Promise<ImageSource>; | ||
showsNumberOfSelectedAssets?: boolean; | ||
/** | ||
* Asynchronously retrieves an ArrayBuffer that represents the raw byte data from this selected image. | ||
* Display prompt text when selecting assets in iOS | ||
*/ | ||
getImageData(): Promise<ArrayBuffer>; | ||
prompt?: string; | ||
/** | ||
* 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. | ||
* Set the number of columns in Portrait in iOS | ||
*/ | ||
data(): Promise<any>; | ||
} | ||
numberOfColumnsInPortrait?: number; | ||
export class ImagePicker { | ||
/** | ||
* Call this before 'present' to request any additional permissions that may be necessary. | ||
* In case of failed authorization consider notifying the user for degraded functionality. | ||
* Set the number of columns in Landscape in iOS | ||
*/ | ||
authorize(): Promise<void>; | ||
numberOfColumnsInLandscape?: number; | ||
/** | ||
* Present the image picker UI. | ||
* The result will be an array of SelectedAsset instances provided when the promise is fulfilled. | ||
* Set the media type (image/video/both) to pick in iOS | ||
*/ | ||
present(): Promise<SelectedAsset[]>; | ||
} | ||
mediaType?: ImagePickerMediaType; | ||
/** | ||
* Provide options for the image picker. | ||
*/ | ||
interface Options { | ||
/** | ||
* Set the picker mode. Supported modes: "single" or "multiple" (default). | ||
*/ | ||
mode?: string; | ||
/** | ||
* Set the text for the done button in iOS | ||
*/ | ||
doneText?: string; | ||
/** | ||
* Set the text for the cancel button in iOS | ||
*/ | ||
cancelText?: string; | ||
/** | ||
* Set the text for the albums button in iOS | ||
*/ | ||
albumsText?: string; | ||
android?: { | ||
@@ -107,9 +76,8 @@ /** | ||
}; | ||
/** | ||
* Indicates images should be sorted newest-first (iOS only, default false). | ||
*/ | ||
newestFirst?: boolean; | ||
} | ||
export function create(options?: Options): ImagePicker; | ||
/** | ||
* @param {Options} [options] - options for the image picker. | ||
* @param {View} [hostView] - [use in iOS] the view that hosts the image picker (e.g. to use when open from a modal page). | ||
*/ | ||
export function create(options?: Options, hostView?: View): ImagePicker; |
{ | ||
"name": "nativescript-imagepicker", | ||
"version": "5.0.2", | ||
"version": "6.0.0", | ||
"description": "A plugin for the NativeScript framework implementing multiple image picker", | ||
@@ -55,7 +55,5 @@ "repository": { | ||
"dependencies": { | ||
"nativescript-ui-core": "^1.0.0", | ||
"nativescript-ui-listview": "^3.5.0", | ||
"nativescript-permissions": "~1.2.3" | ||
}, | ||
"bootstrapper": "nativescript-plugin-seed" | ||
} | ||
} |
@@ -9,3 +9,3 @@ # NativeScript Image Picker ![apple](https://cdn3.iconfinder.com/data/icons/picons-social/57/16-apple-32.png) ![android](https://cdn4.iconfinder.com/data/icons/logos-3/228/android-32.png) | ||
Imagepicker plugin supporting both single and multiple selection. | ||
<br />Plugin supports **iOS8+** and uses [Photos Framework](https://developer.apple.com/library/prerelease/ios//documentation/Photos/Reference/Photos_Framework/index.html). | ||
<br />Plugin supports **iOS8+** and uses [QBImagePicker](https://github.com/questbeat/QBImagePicker) cocoa pod. | ||
<br />For **Android** it uses Intents to open the stock images or file pickers. For Android 6 (API 23) and above the permissions to read file storage should be explicitly required. See demo for implementation details. | ||
@@ -17,2 +17,3 @@ | ||
- [Configuration](#configuration) | ||
- [Migrating from 5.x.x to 6.x.x](#migrating-from-5xx-to-6xx) | ||
- [Migrating from 4.x.x to 5.x.x](#migrating-from-4xx-to-5xx) | ||
@@ -46,2 +47,5 @@ - [Migrating from 3.x.x to 4.x.x](#migrating-from-3xx-to-4xx) | ||
## Migrating from 5.x.x to 6.x.x | ||
With version **6.x.x** the dependency to the `nativescript-ui-listview` plugin is removed and for iOS the [QBImagePicker](https://github.com/questbeat/QBImagePicker) cocoa pod is used. Now the plugin supports some new features, fixes some bugs and looks super native for iOS. You can remove any dependencies to `nativescript-pro-ui`, `nativescript-ui-listview`, etc. in case you've added them in your app specifically for this plugin. Also the options **doneText**, **cancelText**, **albumsText**, **newestFirst** and the methods **cancel()** and **done()** are no longer applicable. The image picker now returns the basic [{N} ImageAsset class](https://github.com/NativeScript/NativeScript/tree/master/tns-core-modules/image-asset) (and not custom asset as before). | ||
## Migrating from 4.x.x to 5.x.x | ||
@@ -122,30 +126,15 @@ With version **5.x.x** major update to the plugin there is a related dependency which needs to be updated inside your project. The plugin uses internally the `nativescript-ui-listview` plugin (part of the NativeScript Pro UI components). Recently the monolithic [NativeScript Pro UI plugin was split in multiple plugins](https://www.nativescript.org/blog/professional-components-from-nativescript-ui-the-big-breakup), each of them representing a single component. Now, instead of the monolithic package, nativescript-imagepicker uses only the component it needs. To use version 5.x.x of the plugin, you need to update any dependencies to `nativescript-pro-ui` in your project with the single component alternatives as described in the [migration guide](http://docs.telerik.com/devtools/nativescript-ui/migration). | ||
| mode | both | multiple | The mode if the imagepicker. Possible values are `single` for single selection and `multiple` for multiple selection. | | ||
| doneText | iOS | Done | The text of the "Done" button on top right. | | ||
| cancelText | iOS | Cancel | The text of the "Cancel" button on top left. | | ||
| albumsText | iOS | Albums | The title of the "Albums" screen from where the selection of album and images can be done. | | ||
| newestFirst | iOS | false | Set to `true` to sort the images in an album by newest first. | | ||
| minimumNumberOfSelection | iOS | 0 | The minumum number of selected assets. | | ||
| maximumNumberOfSelection | iOS | 0 | The maximum number of selected assets. | | ||
| showsNumberOfSelectedAssets | iOS | True | Display the number of selected assets. | | ||
| prompt | iOS | undefined | Display prompt text when selecting assets. | | ||
| numberOfColumnsInPortrait | iOS | 4 | Set the number of columns in Portrait orientation. | | ||
| numberOfColumnsInLandscape | iOS | 7 | Set the number of columns in Landscape orientation. | | ||
| mediaType | iOS | Any | Choose whether to pick Image/Video/Any type of assets. | | ||
The **hostView** parameter can be set to the view that hosts the image picker. Applicable in iOS only, intended to be used when open picker from a modal page. | ||
* authorize() - request the required permissions. | ||
* present() - show the albums to present the user the ability to select images. Returns an array of the selected images. | ||
* cancel() - cancel selection. iOS only. | ||
* done() - confirm the selection is ready. iOS only. | ||
### Properties | ||
| Property | Default | Description | | ||
| --- | --- | --- | | ||
| selection | null | An array of selected image assets. | | ||
| albums | null | Albums from where the images are picked. | | ||
### Image properties | ||
Once image is picked some options can be applied to it before it is used: | ||
| Option | Default | Description | | ||
| --- | --- | --- | | ||
| maxWidth | null | Image max width | | ||
| maxHeight | null | Image max height | | ||
| aspectRatio | fit | iOS only. Possible values are `fit` and `fill`. [Read more](https://developer.apple.com/documentation/photos/phimagecontentmode) | | ||
## Contribute | ||
@@ -152,0 +141,0 @@ We love PRs! Check out the [contributing guidelines](CONTRIBUTING.md). If you want to contribute, but you are not sure where to start - look for [issues labeled `help wanted`](https://github.com/NativeScript/nativescript-imagepicker/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22). |
/// <reference path="./node_modules/tns-platform-declarations/ios.d.ts" /> | ||
/// <reference path="./node_modules/tns-platform-declarations/android.d.ts" /> | ||
/// <reference path="./typings/objc!QBImagePickerController.d.ts" /> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1
17
55828
619
144
2
- Removednativescript-ui-core@^1.0.0
- Removednativescript-ui-listview@^3.5.0
- Removednativescript-ui-core@1.0.12.0.1(transitive)
- Removednativescript-ui-listview@3.8.0(transitive)