iPhone: Mask UIImageView of differing dimensions into square dimension

社会主义新天地 提交于 2019-12-11 12:46:13

问题


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)


回答1:


  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:




回答2:


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


来源:https://stackoverflow.com/questions/4789067/iphone-mask-uiimageview-of-differing-dimensions-into-square-dimension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!