Git Product home page Git Product logo

nl.fokkezb.cachedimageview's Introduction

NOTE: Titanium has built-in remote image caching for Android since 3.1 and for iOS since the early days. This is not documented, but a ticket I created for this to be done has been resolved recently. For this reason I no longer maintain this widget.

CachedImageView Widget Titanium Alloy

Overview

The CachedImageView widget implements the best practice of caching remote images for Titanium Alloy by Appcelerator.

Features

  • Initialize the widget directly through the requiring view.
  • Provide a seperate URL for the image to use on IOS retina devices.
  • Provide a local filename to be used instead of the MD5 hash of the URL.
  • Provide an extension for the local file if the remote doesn't have one.
  • Eventing.

Future work

  • Provide a subdirectory to save the image under.
  • Provide a choice to which system directory (cache, data) to save under.
  • Provide a maximum age for the cached image before re-downloading it.

Quick Start

Get it gitTio

Download this repository and consult the Alloy Documentation on how to install it, or simply use the gitTio CLI:

$ gittio install nl.fokkezb.cachedImageView

Use it

Use the widget in a view just like you'd use an ImageView. Only use Widget instead of ImageView and add the src attribute to point to the widget. Optionally add any of the additional parameters as attributes.

<Widget src="nl.fokkezb.cachedImageView"
id="civ" image="http://url.to/image.png" onClick="handleClick" />

Additonal parameters

The only required parameter is the image parameter. All parameters are passed on to the resulting Ti.UI.ImageView. You can add the following additional parameters to change the widget's behaviour. They can be used both as attributes in <Widget> and when initializing the widget in your controller.

Parameter Type Description
cacheHires string URL of the image to use for iOS retina devices.
cacheName string Basename for the local file instead of the MD5 hash of the URL. Use it when the URL contains some time-dependant key.
cacheExt string Extension for the local file if the URL doesn't have one, like with generated images.
cacheNot string Disable caching

Methods

Method Params Description
init object Any ImageView parameters plus the additional above
applyProperties object Alias for init
setImage string Alias for calling init with only an image parameter
getImage bool returnPath Return the (path to the) local image. Calling this method before it has been cached will return undefined
on / addEventListener string name, function callback Add an eventlistener
off / removeEventListener string name, function callback Remove an eventlistener
trigger / fireEvent string name, object args Fire an event

Properties

Property Description
image Alias to setImage and getImage methods

Initialization in the Controller

If you don't include the image parameter as an attribute in <Widget/>, the resulting Ti.UI.ImageView will not be automatically initialized for you. This allows you to do this yourself in your controller. This can be usefull if you want to add some advanced decission logic to determine which image to use. If this would only depend on the formFactor and platform however, I would recommend using conditional code in <Widget/> instead.

NOTE: The $.cid in the example below corresponds to the id attribute in the <Widget/> example. You can change it to whatever value. From 1.2 on you can also call the more common applyProperties.

$.cid.init({
    image: OS_IOS ? 'http://url.to/image.png' : 'http://url.to/android-image.png'
});

Styling the ImageView

You can style the resulting Ti.UI.ImageView by applying the styles to the <Widget/> instead. Add the style declaration to the TTS file that belongs to the view where you've added the <Widget/>. The styling will be passed on to the resuling Ti.UI.ImageView. Be aware that any attributes you add directly to <Widget/> will override the TTS style.

NOTE: The $.cid in the example below corresponds to the id attribute in the <Widget/> example.

"#cid": {
	width: 57,
	height: 57,
	preventDefaultImage: true
}

Changelog

  • 1.4: Added events
  • 1.3: Fixed bug when using blob/file, added image property and cacheNot param
  • 1.2: Added getImage, setImage, applyProperties and deleted __parentSybmol
  • 1.1: Support for styling via TSS before setting image via init()
  • 1.0: Initial version

License

Copyright 2013 Fokke Zandbergen

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

nl.fokkezb.cachedimageview's People

Contributors

fokkezb avatar

Stargazers

Nick avatar Manjunath (MJ) avatar Antonio Duran avatar Adam T Armstrong avatar Haroon Abbasi avatar Max avatar  avatar Romeo Manzur avatar Nuno Costa avatar Ítalo Matos avatar Eduardo Silva avatar Clem Blanco avatar Abdulatif Henno avatar Afranio Martins avatar Jeff avatar Bruno César avatar  avatar Daniel Waardal avatar  avatar Hugo Nogueira avatar Steven avatar Achraf Bouyakhsass avatar Brams avatar Brenton House avatar samuele coppede avatar Benjamin Clay avatar Rafael K. Streit avatar Brad Pillow avatar HaberdasheryJS avatar Rodrigo Negrete avatar Matthew Lanham avatar Braden Powers avatar James Young avatar  avatar KITAO Masato avatar Alco avatar Justin Lee avatar Luca Fregoso avatar Zaher Ghaibeh avatar Miles avatar Nathan Fitzsimmons avatar Iñaki Abete avatar John Oliva avatar Javier Rayon avatar Imobach Martin avatar  avatar Damien Laureaux avatar Denver avatar Keigo AOKI avatar Mads Møller Schrøder avatar Chris Stevens avatar Tatsuya Arai avatar Jon Ege Ronnenberg avatar

Watchers

zbage avatar Nuno Costa avatar  avatar James Cloos avatar Damien Laureaux avatar  avatar Ítalo Matos avatar

nl.fokkezb.cachedimageview's Issues

Bug: Hires image does not load (resize) correctly

use case:
I have a single retina image I want to use for both non-retina and retina displays.
I do not specify a width and height, so to my users can change this.
result: The image will load a original size the very first, instead of 50% as it should. Once it has been downloaded and the widget will load from local, then the widget works fine.

xml: < Widget src="nl.fokkezb.cachedImageView" id="imageView" / >

controller:
$.imageView.init({
image: "http://www.photographyblogger.net/wp-content/uploads/2010/05/flower29.jpg",
cacheHires: "http://www.photographyblogger.net/wp-content/uploads/2010/05/flower29.jpg",
hires:true
});

tss:
"#imageView":{
touchEnabled:false
}

android fatal error and images folder

image saving on android create an fatal error!

and for the image folder you can use these lines 👍 :

var imageDir = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,
'downloaded_images');
if (! imageDir.exists()) {
     imageDir.createDirectory();
}

savedFile = Ti.Filesystem.getFile(imageDir.getNativePath(), args.cacheName  '.' + args.cacheExtension);

Error when using cachedImageView in XML ListVIew template

Getting the following error:

[ERROR] : : Cannot read property 'symbol' of undefined
[ERROR] : Alloy compiler failed

This only happens when i include, the widget within an itemTemplate:

<Widget src="nl.fokkezb.cachedImageView" id="photo" />

Full example:

<ListView id="listView" left="10dp" right="10dp" top="45dp" bottom="94dp" separatorColor="transparent" backgroundColor="transparent" defaultItemTemplate="myTemplate">
    <Templates>
        <ItemTemplate backgroundColor="#EEEEEE" name="myTemplate" height="80dp">
            <!-- <ImageView bindId="photo" id="image" width="60dp" height="60dp" left="10dp" top="10dp" /> -->
            <Widget src="nl.fokkezb.cachedImageView" id="photo" />
            <ImageView bindId="arrow" image="/directory_arrow.png" width="33dp" height="33dp" right="10dp" />
            <Label class="pubListingTitle" bindId="title" color="#222222" left="80dp" top="10dp" />
            <Label class="pubListingSubTitle" bindId="sub_title" color="#999999" left="80dp" top="30dp" />
            <View bindId="border" backgroundColor="#CBDADA" left="0" right="0" bottom="0" height="5dp" width="Ti.UI.FILL" />
        </ItemTemplate>
    </Templates>

    <ListSection id="defaultSection"></ListSection>
</ListView>

Event Binding not working

I tried binding onClick and onDoubletap to widget like this:-

<Widget src="nl.fokkezb.cachedImageView" id="img1" onDoubletap="showFullImage" onClick="voteMe"/>

But its not calling the showFullImage and voteMe functions on doubleTap and click respectively.

Please let me know if there is any other way to bind events

cacheNot property doesn´t work

I have a big problem with automatic cache of imageView on iOS devices...
So, I´m adding this widget to my app but the init property cacheNot doesn´t work

var args = { cacheNot: true };
		
var myPhoto = Alloy.createWidget("cachedImageView", null, args);
myPhoto.applyProperties({
	width : 160,
	height : 120,
	image : thumbURL,
	touchEnabled : false
});

What I´m doing wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.