Nic Hubbard
Nic Hubbard

Reputation: 42139

iPhone: Mask UIImageView of differing dimensions into square dimension

I have a bunch of UIImageViews that are in different proportions. Some of 100x101 some are 130x121.

How can I mask these to 80x80 and NOT stretch the images? I basically just want to mask a square out of each one. (kind of like the Apple's Photo thumbnail view does)

Upvotes: 1

Views: 1761

Answers (2)

vfn
vfn

Reputation: 6066

Set its content mode UIViewContentMode, you may be looking for UIViewContentModeScaleAspectFit or UIViewContentModeScaleAspectFill.

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setImage:[UIImage imageNamed:@"myImage.png"];
.
.
.

Upvotes: 0

Di Wu
Di Wu

Reputation: 6448

  1. Set the image view's size to 80 x 80
  2. set the image view's contentMode property to UIViewContentModeScaleAspectFill
  3. Finally, to make round corners, use the following code, and import QuartzCore/QuartzCore.h at the beginning of your implementation file.

    CALayer * layer = [myImageView layer];
    [layer setMasksToBounds:YES];
    [layer setCornerRadius:12.0f];
    

Edited: Yes, by saying size I mean frame, the W and H:

enter image description here

Upvotes: 5

Related Questions