Skip to content

Instantly share code, notes, and snippets.

@roblav96
Created December 27, 2016 09:52
Show Gist options
  • Select an option

  • Save roblav96/0bbc7fe6aaa47c6e1cad46250609da83 to your computer and use it in GitHub Desktop.

Select an option

Save roblav96/0bbc7fe6aaa47c6e1cad46250609da83 to your computer and use it in GitHub Desktop.
iimage
//
import common = require('./iimage.common')
import * as application from 'application'
import { View } from 'ui/core/view'
import { Property, PropertyMetadataSettings, PropertyChangeData } from 'ui/core/dependency-observable'
import { PropertyMetadata } from 'ui/core/proxy'
import { Stretch } from 'ui/enums'
import { ImageSource } from 'image-source'
import { knownFolders, path } from 'file-system'
import { fromNativeSource, fromFile, fromResource } from 'image-source'
import { ad, RESOURCE_PREFIX } from 'utils/utils'
function onImageSourcePropertyChanged(args: PropertyChangeData) {
// global.tnsconsole.log('onImageSourcePropertyChanged', args.newValue)
let image = <IImage>args.object
if (args.newValue && image.android) {
image._setNativeImage(args.newValue)
}
}
(<PropertyMetadata>common.IImage.srcProperty.metadata).onSetNativeValue = onImageSourcePropertyChanged
function onStretchPropertyChanged(args: PropertyChangeData) {
// global.tnsconsole.log('onStretchPropertyChanged', args.newValue)
let image = <IImage>args.object
if (image.android) {
switch (args.newValue) {
case Stretch.aspectFit:
image.android.setScaleType(android.widget.ImageView.ScaleType.FIT_CENTER)
break
case Stretch.aspectFill:
image.android.setScaleType(android.widget.ImageView.ScaleType.CENTER_CROP)
break
case Stretch.fill:
image.android.setScaleType(android.widget.ImageView.ScaleType.FIT_XY)
break
case Stretch.none:
default:
image.android.setScaleType(android.widget.ImageView.ScaleType.MATRIX)
break
}
}
}
(<PropertyMetadata>common.IImage.stretchProperty.metadata).onSetNativeValue = onStretchPropertyChanged
export class IImage extends common.IImage {
public glide: com.bumptech.glide.RequestManager
private _android: android.widget.ImageView
constructor() {
super()
// global.tnsconsole.info('IImage > constructor')
}
get android(): android.widget.ImageView {
return this._android
}
public _createUI() {
// global.tnsconsole.warn('_createUI')
this._android = new android.widget.ImageView(this._context)
// this._android.setScaleType(android.widget.ImageView.ScaleType.FIT_XY)
// this._android.setMinimumWidth(1024)
// this._android.setMinimumHeight(1024)
// this._android.setLayoutParams()
}
public _setNativeImage(image: string & ImageSource) {
// global.tnsconsole.log('_setimage', image)
if (typeof image != 'string') {
let bitmap: android.graphics.Bitmap = image.android
this._android.setImageBitmap(bitmap)
} else {
if (image.indexOf(RESOURCE_PREFIX) == 0) {
image = ad.resources.getDrawableId(image.substring(6))
image = new java.lang.Integer(image.toString()) as any
}
this.glide = com.bumptech.glide.Glide.with(this._context)
this.glide.load(image).into(this._android)
}
// global.tnsconsole.log('this.width', this.width)
// global.tnsconsole.log('this.height', this.height)
// global.tnsconsole.log('this.android.getMeasuredWidth()', this.android.getMeasuredWidth())
// global.tnsconsole.log('this.android.getMeasuredHeight()', this.android.getMeasuredHeight())
// global.tnsconsole.log('this.parent.getMeasuredWidth()', this.parent.getMeasuredWidth())
// global.tnsconsole.log('this.parent.getMeasuredHeight()', this.parent.getMeasuredHeight())
// global.tnsconsole.log('this.parent.parent.getMeasuredWidth()', this.parent.parent.getMeasuredWidth())
// global.tnsconsole.log('this.parent.parent.getMeasuredHeight()', this.parent.parent.getMeasuredHeight())
// global.tnsconsole.log('this.parent.parent.parent.getMeasuredWidth()', this.parent.parent.parent.getMeasuredWidth())
// global.tnsconsole.log('this.parent.parent.parent.getMeasuredHeight()', this.parent.parent.parent.getMeasuredHeight())
// let width: number = 16
// if (this.width > 0) {
// width = this.width
// } else if (this.parent.getMeasuredWidth() > 0) {
// width = this.parent.getMeasuredWidth()
// }
// global.tnsconsole.log('width', width)
// let height: number = 16
// if (this.height > 0) {
// height = this.height
// } else if (this.parent.getMeasuredHeight() > 0) {
// height = this.parent.getMeasuredHeight()
// }
// global.tnsconsole.log('height', height)
// this.glide.override(width, height)
}
}
//
import * as application from 'application'
import { View } from 'ui/core/view'
import { Property, PropertyMetadataSettings, PropertyChangeData } from 'ui/core/dependency-observable'
import { PropertyMetadata } from 'ui/core/proxy'
import { Stretch } from 'ui/enums'
import { device, platformNames } from 'platform'
const AffectsLayout = device.os === platformNames.android ? PropertyMetadataSettings.None : PropertyMetadataSettings.AffectsLayout
export class IImage extends View {
public static srcProperty = new Property('src', 'IImage', new PropertyMetadata(undefined, PropertyMetadataSettings.None))
public static stretchProperty = new Property('stretch', 'IImage', new PropertyMetadata(Stretch.aspectFit, AffectsLayout))
constructor() {
super()
}
get src(): string {
return this._getValue(IImage.srcProperty)
}
set src(value: string) {
this._setValue(IImage.srcProperty, value)
}
get stretch(): string {
return this._getValue(IImage.stretchProperty)
}
set stretch(value: string) {
this._setValue(IImage.stretchProperty, value)
}
}
//
import common = require('./iimage.common')
import * as application from 'application'
import { View } from 'ui/core/view'
import { Image } from 'ui/image'
import { Property, PropertyMetadataSettings, PropertyChangeData } from 'ui/core/dependency-observable'
import { PropertyMetadata } from 'ui/core/proxy'
import { Stretch } from 'ui/enums'
import { knownFolders, path, File } from 'file-system'
import { fromNativeSource, fromFile, fromResource } from 'image-source'
import { ImageFormat } from 'ui/enums'
import { RESOURCE_PREFIX } from 'utils/utils'
function onImageSourcePropertyChanged(args: PropertyChangeData) {
let image = <IImage>args.object
if (!image.ios) {
return
}
image._setNativeImage(args.newValue)
}
(<PropertyMetadata>common.IImage.srcProperty.metadata).onSetNativeValue = onImageSourcePropertyChanged
function onStretchPropertyChanged(args: PropertyChangeData) {
let image = <IImage>args.object
if (!image.ios) {
return
}
switch (args.newValue) {
case Stretch.aspectFit:
image.ios.contentMode = UIViewContentMode.ScaleAspectFit
break
case Stretch.aspectFill:
image.ios.contentMode = UIViewContentMode.ScaleAspectFill
break
case Stretch.fill:
image.ios.contentMode = UIViewContentMode.ScaleToFill
break
case Stretch.none:
default:
image.ios.contentMode = UIViewContentMode.TopLeft
break
}
}
(<PropertyMetadata>common.IImage.stretchProperty.metadata).onSetNativeValue = onStretchPropertyChanged
export class IImage extends common.IImage {
private _ios: UIImageView
constructor() {
super()
this._ios = UIImageView.new()
this._ios.contentMode = UIViewContentMode.ScaleAspectFit
}
get ios(): UIImageView {
return this._ios
}
public _setNativeImage(image: string) {
if (!image) {
return
}
// global.tnsconsole.warn('_setNativeImage', image)
let rgx = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/
let url: NSURL
if (rgx.test(image)) {
url = NSURL.URLWithString(image)
} else if (image.indexOf(RESOURCE_PREFIX) == 0) {
image = image.substring(6)
let file = path.join(knownFolders.documents().path, image + '_.png')
// global.tnsconsole.log('file', file)
if (!File.exists(file)) {
// global.tnsconsole.log('fromResource', image)
fromResource(image).saveToFile(file, ImageFormat.png)
}
url = NSURL.fileURLWithPath(file)
} else {
if (image.indexOf('file:///') == 0) {
image = image.substring(7)
}
url = NSURL.fileURLWithPath(image)
}
// global.tnsconsole.log('url.description', url.description)
(<any>this._ios).yy_setImageWithURLOptions(url, YYWebImageOptions.AllowBackgroundTask)
}
}
//
import common = require('./iimage.common')
import * as application from 'application'
import { View } from 'ui/core/view'
export declare class IImage extends common.IImage {
public android
public ios
public _createUI()
public _setNativeImage(image: string)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment