Titanium Community Questions & Answer Archive

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

ImageView Scaling Mode

It seems that ImageViews by default will scale their content to fit within the width/height that you set, but this often times will result in an image that does not fill the view entirely (this scaling mode is called "Aspect Fit" in iPhone SDK).

Is it posible to have the ImageView fill the available space instead (cropping the image so that the full width/height of the ImageView are filled, this scaling mode is called "Aspect Fill" in iPhone SDK).

I know this is posible with the native iPhone SDK which offers several scaling modes for images, but cant find a way to set this in titanium.

— asked May 10th 2010 by Get Glue
  • imageview
  • iphone
  • mobile
  • scale
0 Comments

9 Answers

  • Dan's answer and the link by Peter answer the question, but I thought I'd combine and expand on the two to make it easier for others.

    Create a 'container' view that holds the image and acts as a mask. Set the height/width to whatever you want the final, cropped image to be. borderRadius: 0 is key because it causes the masking so that the image doesn't overflow.

    var container =  Titanium.UI.createView({
      height:       100
      width:        100
      borderRadius: 0
      ...
    });
    

    Assuming the native resolution of the image is 200x300, then you can do one of two things depending on what you want the outcome to be:

    var image =  Titanium.UI.createImageView({
      url:    whatever
      height: 200
      width:  300
      });
    container.add(image);
    

    This will extract a 100x100 piece from the center of the image without scaling. Cropping 50px from the top/bottom and 100px from the left/right.

    var image =  Titanium.UI.createImageView({
      url:    whatever
      height: 100
      width:  150
    });
    container.add(image);
    

    This will scale the image to 100x150 and then the container will extract 100x100 from the center. In effect cropping 25px from the left/right, but the leaving the height intact. You'll probably want a helper function that calculates the scaled height/width values.

    Obviously, an Aspect Fill scaling method would be less hassle, but at least it works.

    — answered July 28th 2011 by Phil Oye
    permalink
    1 Comment
    • Worked +1

      — commented May 6th 2012 by Jafar Amjad
  • I've put together a quick-and-dirty extension to Ti.UI.ImageView for iOS which adds support for aspect fit and aspect fill content modes as well as the clipsToBounds property. Here's an example of how this would be used to achieve the same effect described in Phil's post:

    var imageview_ex = require('com.obscure.imageview_ex');
    var imageView = imageview_ex.createImageView({
      height: 100,
      width: 100,
      hires: true,
      contentMode: 'aspectfill',
      clipsToBounds: true,
      image: whatever
    });
    window.add(imageView);
    

    The main advantage of this extension over the JavaScript method is that you don't need to know the aspect ratio of the image ahead of time in order to scale down and use an aspect fill.

    — answered September 29th 2011 by Paul Mietz Egli
    permalink
    2 Comments
    • Hi, having some issues building this… Do you have a build available (built module) ?

      Thanks

      — commented October 11th 2011 by Bill Bixby
    • Yes, there is a prebuilt module zip on Github here.

      — commented October 12th 2011 by Paul Mietz Egli
  • This seemed to work for me:
    http://developer.appcelerator.com/question/118677/masking-or-hiding-overflow-from-a-view.html#answer-207390

    — answered June 22nd 2011 by Peter Wiley
    permalink
    0 Comments
  • I need this also.

    — answered October 26th 2010 by Steven Day
    permalink
    0 Comments
  • any updates on this? same issue here.

    — answered February 1st 2011 by Peter Levin
    permalink
    0 Comments
  • any updates on this?

    — answered February 1st 2011 by Peter Levin
    permalink
    0 Comments
  • Yes, I need this for my image thumb grid.

    — answered August 13th 2010 by Sindre Sorhus
    permalink
    0 Comments
  • You might want to create a view that holds the image, and calculate the image size to fill that view, and so a cropping will appear.

    let's say you have an image 200x300, and you set the view at 100x100 you will scale the image at 100x150 and put it inside the view, centered.

    — answered August 14th 2010 by Dan Tamas
    permalink
    1 Comment
    • This doesn't seem to restrict the image to the imageview though… the image would leak over the two sides of the imageview… how can you make sure the imageview actually crops any overflow?

      — commented September 19th 2010 by Michael Steuer
  • Phil's answer works like a charm! Thanks. Needed this.

    Any tips though on how to create rounded corners though? borderRadius on either the container or image doesn't have any effect.

    — answered August 31st 2011 by Gino Bruijn
    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.