Skip to content

Commit

Permalink
feat(ImageSource): support ImageSource object properly
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanWalker committed Mar 27, 2018
1 parent ebfe2fd commit c77c829
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 502 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ publish/src
publish/package
demo/report/report.html
demo/report/stats.json
src/package-lock.json
7 changes: 7 additions & 0 deletions demo/app/main-page.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as observable from 'tns-core-modules/data/observable';
import * as pages from 'tns-core-modules/ui/page';
import { ImageSource, fromResource } from 'tns-core-modules/image-source';
import { HelloWorldModel } from './main-view-model';
import { ImageZoom } from 'nativescript-image-zoom';

Expand All @@ -23,3 +24,9 @@ export function loadResource() {
image.src = 'res://image_302063';
}
}

export function loadImgSrc() {
if (image) {
image.src = <any>fromResource('image_302063');
}
}
3 changes: 2 additions & 1 deletion demo/app/main-page.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded" class="page"
xmlns:ui="nativescript-image-zoom">
<GridLayout rows="auto,*,auto,auto" class="p-20">
<GridLayout rows="auto,*,auto,auto,auto" class="p-20">
<Label text="Zoom" class="t-20 text-center c-black" textWrap="true"/>
<ui:ImageZoom id="image" row="1" src="~/images/148080.jpg" maxZoom="6" />
<Button row="2" text="Remote Source" tap="loadRemoteSrc"/>
<Button row="3" text="Local Resource" tap="loadResource"/>
<Button row="4" text="Load ImageSource" tap="loadImgSrc"/>
</GridLayout>
</Page>
11 changes: 8 additions & 3 deletions src/image-zoom.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ export class ImageZoom extends ImageZoomBase {
[srcProperty.setNative](src: string) {
if (!src) return;
let source;
if (src.startsWith('~/')) {
const isString = typeof src === 'string';
if (isString && src.startsWith('~/')) {
const uri = fs.path.join(
fs.knownFolders.currentApp().path,
src.replace('~/', '')
);
source = com.davemorrissey.labs.subscaleview.ImageSource.uri(uri);
} else if (src.startsWith('res://')) {
} else if (isString && src.startsWith('res://')) {
const name = src.replace('res://', '');
const identifier: number = utils.ad
.getApplication()
Expand All @@ -55,13 +56,17 @@ export class ImageZoom extends ImageZoomBase {
source = com.davemorrissey.labs.subscaleview.ImageSource.resource(
identifier
);
} else if (src.startsWith('http')) {
} else if (isString && src.startsWith('http')) {
imageSource.fromUrl(src).then((data: imageSource.ImageSource) => {
source = com.davemorrissey.labs.subscaleview.ImageSource.bitmap(
data.android
);
this.nativeView.setImage(source);
});
} else if (typeof src === 'object') {
source = com.davemorrissey.labs.subscaleview.ImageSource.bitmap(
(<imageSource.ImageSource>src).android
);
} else {
source = com.davemorrissey.labs.subscaleview.ImageSource.uri(src);
}
Expand Down
2 changes: 1 addition & 1 deletion src/image-zoom.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const maxZoomScaleProperty = new Property<ImageZoomBase, number>({
defaultValue: 4
});

export const srcProperty = new Property<ImageZoomBase, string>({
export const srcProperty = new Property<ImageZoomBase, any>({
name: 'src'
});

Expand Down
2 changes: 1 addition & 1 deletion src/image-zoom.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class ImageZoom extends ScrollView {
}

[srcProperty.setNative](src: string) {
if (src.startsWith('res://')) {
if (typeof src === 'string' && src.startsWith('res://')) {
this._image.imageSource = imageSource.fromNativeSource(
UIImage.imageNamed(src.replace('res://', ''))
);
Expand Down
Loading

0 comments on commit c77c829

Please sign in to comment.