Skip to content

Commit 82d8d79

Browse files
committed
Implement getImage() and getImageData()
1 parent 9b9a98a commit 82d8d79

File tree

1 file changed

+57
-18
lines changed

1 file changed

+57
-18
lines changed

source/viewmodel.ios.ts

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -180,18 +180,6 @@ export class Asset extends SelectedAsset {
180180
return this._thumb;
181181
}
182182

183-
getImage(options?: ImageOptions): Promise<image_source.ImageSource> {
184-
return new Promise<image_source.ImageSource>((resolve, reject) => {
185-
186-
});
187-
}
188-
189-
getImageData(): Promise<ArrayBuffer> {
190-
return new Promise<ArrayBuffer>((resolve, reject) => {
191-
192-
});
193-
}
194-
195183
get selected(): boolean {
196184
return !!this._selected;
197185
}
@@ -299,11 +287,52 @@ class ImagePickerPH extends ImagePicker {
299287
}
300288

301289
createPHImageThumb(target, asset: PHAsset): void {
302-
PHImageManager.defaultManager().requestImageForAssetTargetSizeContentModeOptionsResultHandler(asset, this._thumbRequestSize, PHImageContentMode.PHImageContentModeAspectFill, this._thumbRequestOptions, function (target, uiImage, info) {
303-
var imageSource = new image_source.ImageSource();
304-
imageSource.setNativeSource(uiImage);
305-
target.setThumb(imageSource);
306-
}.bind(this, target));
290+
PHImageManager.defaultManager().requestImageForAssetTargetSizeContentModeOptionsResultHandler(asset, this._thumbRequestSize, PHImageContentMode.PHImageContentModeAspectFill,
291+
this._thumbRequestOptions, function (target, uiImage, info) {
292+
var imageSource = new image_source.ImageSource();
293+
imageSource.setNativeSource(uiImage);
294+
target.setThumb(imageSource);
295+
}.bind(this, target));
296+
}
297+
298+
/**
299+
* Creates a new ImageSource from the given image, using the given sizing options.
300+
* @param image The image asset that should be put into an ImageSource.
301+
* @param options The options that should be used to create the ImageSource.
302+
*/
303+
createPHImage(image: PHAsset, options?: ImageOptions): Promise<image_source.ImageSource> {
304+
return new Promise<image_source.ImageSource>((resolve, reject) => {
305+
var size: CGSize = options ? CGSizeMake(options.maxWidth, options.maxHeight) : PHImageManagerMaximumSize;
306+
var resizeMode = PHImageRequestOptions.alloc().init();
307+
308+
// TODO: Decide whether it is benefical to use PHImageRequestOptionsResizeModeFast
309+
// Accuracy vs Performance. It is probably best to expose these as iOS specific options.
310+
resizeMode.resizeMode = PHImageRequestOptionsResizeMode.PHImageRequestOptionsResizeModeExact;
311+
resizeMode.synchronous = false;
312+
313+
// TODO: provide the ability to change this setting.
314+
// Right now, it is needed to make sure that resolve is not called twice.
315+
resizeMode.deliveryMode = PHImageRequestOptionsDeliveryMode.PHImageRequestOptionsDeliveryModeHighQualityFormat;
316+
resizeMode.normalizedCropRect = CGRectMake(0, 0, 1, 1);
317+
PHImageManager.defaultManager().requestImageForAssetTargetSizeContentModeOptionsResultHandler(
318+
image,
319+
size,
320+
PHImageContentMode.PHImageContentModeAspectFill,
321+
resizeMode,
322+
(createdImage, data) => {
323+
if (createdImage) {
324+
var imageSource = new image_source.ImageSource();
325+
imageSource.setNativeSource(createdImage);
326+
327+
// TODO: Determine whether runOnRunLoop is needed
328+
// for callback or not. (See the data() implementation in AssetPH below)
329+
resolve(imageSource);
330+
} else {
331+
reject(new Error("The image could not be created."));
332+
}
333+
}
334+
);
335+
});
307336
}
308337

309338
done(): void {
@@ -376,6 +405,16 @@ class AssetPH extends Asset {
376405
return this._phAsset.localIdentifier.toString();
377406
}
378407

408+
getImage(options?: ImageOptions): Promise<image_source.ImageSource> {
409+
return (<ImagePickerPH>(<AlbumPH>this.album).imagePicker).createPHImage(this._phAsset, options);
410+
}
411+
412+
getImageData(): Promise<ArrayBuffer> {
413+
return this.data().then((data: NSData) => {
414+
return (<any>interop).bufferFromData(data);
415+
});
416+
}
417+
379418
get fileUri(): string {
380419
if (!AssetPH._uriRequestOptions) {
381420
AssetPH._uriRequestOptions = PHImageRequestOptions.alloc().init();
@@ -392,7 +431,7 @@ class AssetPH extends Asset {
392431
return undefined;
393432
}
394433

395-
data(): Thenable<any> {
434+
data(): Promise<any> {
396435
return new Promise((resolve, reject) => {
397436
var runloop = CFRunLoopGetCurrent();
398437
PHImageManager.defaultManager().requestImageDataForAssetOptionsResultHandler(this._phAsset, null, (data, dataUTI, orientation, info) => {

0 commit comments

Comments
 (0)