iPhone: Mask UIImageView of differing dimensions i

2019-08-14 20:32发布

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)

2条回答
放荡不羁爱自由
2楼-- · 2019-08-14 20:37
  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

查看更多
forever°为你锁心
3楼-- · 2019-08-14 20:49

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"];
.
.
.
查看更多
登录 后发表回答