Skip to content

Commit b6747d3

Browse files
author
radeva
authored
Merge pull request #118 from NativeScript/radeva/fix-aspect-ratio
(feat): Add option for iOS image aspect ratio. Default is `fill`.
2 parents cba4ab1 + f732674 commit b6747d3

File tree

6 files changed

+34
-5
lines changed

6 files changed

+34
-5
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ context
122122
| albums | null | Albums from where the images are picked. |
123123
124124
125+
### Image properties
126+
127+
Once image is picked some options can be applied to it before it is used:
128+
129+
| Option | Default | Description |
130+
| --- | --- | --- |
131+
| maxWidth | null | Image max width |
132+
| maxHeight | null | Image max height |
133+
| aspectRatio | fit | iOS only. Possible values are `fit` and `fill`. [Read more](https://developer.apple.com/documentation/photos/phimagecontentmode) |
134+
125135
## License
126136
127137
2015, Telerik AD

demo/app/main-page.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,25 @@ import { isAndroid } from "tns-core-modules/platform";
44
import * as imagepicker from "nativescript-imagepicker";
55

66
let list;
7+
let imageSrc;
78

89
export function pageLoaded(args: EventData) {
910
let page = <Page>args.object;
1011
list = page.getViewById("urls-list");
12+
imageSrc = page.getViewById("imageSrc");
1113
}
1214

1315
export function onSelectMultipleTap(args) {
1416
let context = imagepicker.create({ mode: "multiple" });
15-
startSelection(context);
17+
startSelection(context, false);
1618
}
1719

1820
export function onSelectSingleTap(args) {
1921
let context = imagepicker.create({ mode: "single" });
20-
startSelection(context);
22+
startSelection(context, true);
2123
}
2224

23-
function startSelection(context) {
25+
function startSelection(context, isSingle) {
2426
context
2527
.authorize()
2628
.then(function() {
@@ -32,6 +34,14 @@ function startSelection(context) {
3234
selection.forEach(function(selected) {
3335
console.log("----------------");
3436
console.log("uri: " + selected.uri);
37+
if (isSingle) {
38+
selected.getImage({ maxWidth: 200, maxHeight: 200, aspectRatio: 'fill' })
39+
.then((imageSource) => {
40+
imageSrc.src = imageSource;
41+
});
42+
} else {
43+
imageSrc.visibility = 'hidden';
44+
}
3545
});
3646
list.items = selection;
3747
}).catch(function (e) {

demo/app/main-page.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
</GridLayout>
1313
</ListView.itemTemplate>
1414
</ListView>
15+
16+
<Image id="imageSrc" width="200" height="200" />
1517
<Button row="1" text="Pick Single" tap="onSelectSingleTap" horizontalAlignment="center" />
1618
<Button row="2" text="Pick Multiple" tap="onSelectMultipleTap" horizontalAlignment="center" />
1719
</GridLayout>

src/imagepicker.ios.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const IMAGE_HEIGHT = 80;
1919
interface ImageOptions {
2020
maxWidth?: number;
2121
maxHeight?: number;
22+
aspectRatio?: "fill" | "fit";
2223
}
2324

2425
export function create(options?): ImagePicker {
@@ -344,6 +345,7 @@ class ImagePickerPH extends ImagePicker {
344345
return new Promise<image_source.ImageSource>((resolve, reject) => {
345346
let size: CGSize = options ? CGSizeMake(options.maxWidth, options.maxHeight) : PHImageManagerMaximumSize;
346347
let resizeMode = PHImageRequestOptions.alloc().init();
348+
let aspectRatio = (options && options.aspectRatio && options.aspectRatio === 'fill') ? PHImageContentMode.AspectFill : PHImageContentMode.AspectFit;
347349

348350
// TODO: Decide whether it is benefical to use PHImageRequestOptionsResizeModeFast
349351
// Accuracy vs Performance. It is probably best to expose these as iOS specific options.
@@ -357,7 +359,7 @@ class ImagePickerPH extends ImagePicker {
357359
PHImageManager.defaultManager().requestImageForAssetTargetSizeContentModeOptionsResultHandler(
358360
image,
359361
size,
360-
PHImageContentMode.AspectFit, // Scales the image so that its larger dimension fits the target size
362+
aspectRatio,
361363
resizeMode,
362364
(createdImage, data) => {
363365
if (createdImage) {

src/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ export interface ImageOptions {
1212
* The maximum height that the image is allowed to be.
1313
*/
1414
maxHeight?: number;
15+
16+
/**
17+
* iOS only. The image aspect ratio. Default value: fit.
18+
*/
19+
aspectRatio?: "fill" | "fit";
1520
}
1621

1722
export class SelectedAsset extends imageAssetModule.ImageAsset {

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-imagepicker",
3-
"version": "3.0.7",
3+
"version": "4.0.0",
44
"description": "A plugin for the NativeScript framework implementing multiple image picker",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)