Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Emulate iPhone Gallery Image View

Goal

I'm trying to build an app that emulates the iPhone gallery's image view. Right now all I'm trying to do is:

  • Scale the image to fit the view
  • Allow pinching to zoom
  • Allow scrolling zoomed images
  • Allow double-tapping to zoom

Current Code

Right now I'm doing:

var imageView = Titanium.UI.createImageView({
    url: '../images/demo.jpg',
    top: 0
});

var b = imageView.toBlob();
var w = (320 / b.width);

var scrollView = Titanium.UI.createScrollView({
    width: 320,
    contentWidth: b.width,
    contentHeight: b.height,
    zoomScale: w,
    contentOffset : {x: 0, y: 0},
    maxZoomScale: 5,
    minZoomScale: w
});

Which gets me somewhat close. The image is scaled to the right size, and pinch/scroll work, but…

Problems

  • The image loads off the screen—you have to scroll to view it (it's just to the right of the screen)
  • The scrollable area is larger than the image when zoomed out (if the image is zoomed to fit the screen, you can scroll it off the screen)
  • There's no double-tapping

Now, I can implement double-tapping myself, but I'm surprised that the image view can't handle this standard behavior. The other two issues I can't quite figure out… Anyone?

— asked April 2nd 2010 by Chris Morrell
  • gallery
  • imageview
  • scrollview
  • ui
1 Comment
  • Does someone have any updates / possible solutions on this?

    — commented February 15th 2011 by Jeroen Kntl

5 Answers

  • There is a wonderful open-source version at Codeboxed.

    — answered May 28th 2011 by Micah Alcorn
    permalink
    0 Comments
  • Did you ever get this working Chris?

    I've been working with basic image manipulation myself, and I can't figure out pinch/zoom in a sane manner.

    — answered April 18th 2010 by Jonathan Barket
    permalink
    0 Comments
  • I figured I would add some of the code I used for displaying the photo gallery I created:

    This code has a bug in which the 0th photo is placed below the 4th photo…

    To display the tiled thumbnails (inside for loop, c is the increment):

    var photoWidth = 75;
    var photoPadSide = 4;
    var leftPixels = 0;
    
                switch (c%4)
                {    
                    case (1):
                        leftPixels = (photoWidth*0) + (photoPadSide*0) + photoPadSide;
                        if ((c/4)>0){    topPixels=60+(photoWidth*((c/4)-1)+photoPadSide*((c/4)-1));    } else {    topPixels=64+photoPadSide; }
                        break;    
                    case (2):
                        leftPixels = (photoWidth*1) + (photoPadSide*1) + photoPadSide;
                        break;                
                    case (3):
                        leftPixels = (photoWidth*2) + (photoPadSide*2) + photoPadSide;
                        break;
                    case (0):
                        leftPixels = (photoWidth*3) + (photoPadSide*3) + photoPadSide;
                        break;
                    default:
                        leftPixels = (photoWidth*0) + (photoPadSide*0) + photoPadSide;
                        if ((c/4)>0){    topPixels=60+(photoWidth*((c/4)-1)+photoPadSide*((c/4)-1));    } else {    topPixels=64+photoPadSide; }
                }
    
    — answered July 20th 2010 by John Welch
    permalink
    0 Comments
  • I'm also looking for something similar to this.

    Has anyone come up with a good code base for this?

    — answered July 19th 2010 by John Welch
    permalink
    0 Comments
  • I have wrote a easy to use plug-in that will just accomplish this, you find the plug-in over here http://codecanyon.net/item/titanium-gallery-module/167909 or check http://kiwiii.nl

    — answered March 14th 2011 by Jason Meulenhoff
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.