开发者

iPhone: Mask UIImageView of differing dimensions into square dimension

开发者 https://www.devze.com 2023-02-06 18:50 出处:网络
I have a bunch of UIImageViews tha开发者_开发百科t are in different proportions.Some of 100x101 some are 130x121.

I have a bunch of UIImageViews tha开发者_开发百科t 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)


  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:

iPhone: Mask UIImageView of differing dimensions into square dimension


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"];
.
.
.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号