Skip to content

Commit 2bb1264

Browse files
authored
Merge pull request #6 from NathanWalker/master
feat(ImageSource): support ImageSource object properly
2 parents ebfe2fd + c77c829 commit 2bb1264

File tree

7 files changed

+20
-502
lines changed

7 files changed

+20
-502
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ publish/src
2222
publish/package
2323
demo/report/report.html
2424
demo/report/stats.json
25+
src/package-lock.json

demo/app/main-page.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as observable from 'tns-core-modules/data/observable';
22
import * as pages from 'tns-core-modules/ui/page';
3+
import { ImageSource, fromResource } from 'tns-core-modules/image-source';
34
import { HelloWorldModel } from './main-view-model';
45
import { ImageZoom } from 'nativescript-image-zoom';
56

@@ -23,3 +24,9 @@ export function loadResource() {
2324
image.src = 'res://image_302063';
2425
}
2526
}
27+
28+
export function loadImgSrc() {
29+
if (image) {
30+
image.src = <any>fromResource('image_302063');
31+
}
32+
}

demo/app/main-page.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded" class="page"
22
xmlns:ui="nativescript-image-zoom">
3-
<GridLayout rows="auto,*,auto,auto" class="p-20">
3+
<GridLayout rows="auto,*,auto,auto,auto" class="p-20">
44
<Label text="Zoom" class="t-20 text-center c-black" textWrap="true"/>
55
<ui:ImageZoom id="image" row="1" src="~/images/148080.jpg" maxZoom="6" />
66
<Button row="2" text="Remote Source" tap="loadRemoteSrc"/>
77
<Button row="3" text="Local Resource" tap="loadResource"/>
8+
<Button row="4" text="Load ImageSource" tap="loadImgSrc"/>
89
</GridLayout>
910
</Page>

src/image-zoom.android.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ export class ImageZoom extends ImageZoomBase {
3636
[srcProperty.setNative](src: string) {
3737
if (!src) return;
3838
let source;
39-
if (src.startsWith('~/')) {
39+
const isString = typeof src === 'string';
40+
if (isString && src.startsWith('~/')) {
4041
const uri = fs.path.join(
4142
fs.knownFolders.currentApp().path,
4243
src.replace('~/', '')
4344
);
4445
source = com.davemorrissey.labs.subscaleview.ImageSource.uri(uri);
45-
} else if (src.startsWith('res://')) {
46+
} else if (isString && src.startsWith('res://')) {
4647
const name = src.replace('res://', '');
4748
const identifier: number = utils.ad
4849
.getApplication()
@@ -55,13 +56,17 @@ export class ImageZoom extends ImageZoomBase {
5556
source = com.davemorrissey.labs.subscaleview.ImageSource.resource(
5657
identifier
5758
);
58-
} else if (src.startsWith('http')) {
59+
} else if (isString && src.startsWith('http')) {
5960
imageSource.fromUrl(src).then((data: imageSource.ImageSource) => {
6061
source = com.davemorrissey.labs.subscaleview.ImageSource.bitmap(
6162
data.android
6263
);
6364
this.nativeView.setImage(source);
6465
});
66+
} else if (typeof src === 'object') {
67+
source = com.davemorrissey.labs.subscaleview.ImageSource.bitmap(
68+
(<imageSource.ImageSource>src).android
69+
);
6570
} else {
6671
source = com.davemorrissey.labs.subscaleview.ImageSource.uri(src);
6772
}

src/image-zoom.common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const maxZoomScaleProperty = new Property<ImageZoomBase, number>({
2828
defaultValue: 4
2929
});
3030

31-
export const srcProperty = new Property<ImageZoomBase, string>({
31+
export const srcProperty = new Property<ImageZoomBase, any>({
3232
name: 'src'
3333
});
3434

src/image-zoom.ios.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class ImageZoom extends ScrollView {
5959
}
6060

6161
[srcProperty.setNative](src: string) {
62-
if (src.startsWith('res://')) {
62+
if (typeof src === 'string' && src.startsWith('res://')) {
6363
this._image.imageSource = imageSource.fromNativeSource(
6464
UIImage.imageNamed(src.replace('res://', ''))
6565
);

0 commit comments

Comments
 (0)