iOS项目开发实战——UIImageView的使用与图片显示模式

      现在我们使用iOS的Image控件来显示一张图片,并设置图片的显示模式,代码如下:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  
  //注意这是加载图片最简单的方式;这张图片会被缓存到缓存中,加载速度较快;
  UIImage *image = [UIImage imageNamed:@"img"];
  
  //图片的显示是需要载体的;需要放在UIImageView;
  UIImageView *imgView = [[UIImageView alloc]init];
  //图片显示在屏幕上的大小是由载体控制的;
  //现在把载体的大小设置成图片的大小,使用图片的大小设置UIImageView的长宽;
  imgView.frame = CGRectMake(10, 100, 300, 500);
  imgView.backgroundColor = [UIColor yellowColor];
  [imgView setImage:image];
  
  [self.view addSubview:imgView];
  
  //图片的显示模式;
  /*
   UIViewContentModeScaleToFill,
   UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
   UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
   UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
   UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
   UIViewContentModeTop,
   UIViewContentModeBottom,
   UIViewContentModeLeft,
   UIViewContentModeRight,
   UIViewContentModeTopLeft,
   UIViewContentModeTopRight,
   UIViewContentModeBottomLeft,
   UIViewContentModeBottomRight,
   
   */
  //内容模式;
  //注意:UIViewContentModeScaleToFill是默认的显示效果;
  /*
   UIViewContentModeScaleToFill:拉伸充满整个载体;
   UIViewContentModeScaleAspectFit:拉伸不改变比例,充满最小的一边;
   UIViewContentModeScaleAspectFill:拉伸不改变比例,充满最大的一边;
   */
  imgView.contentMode =  UIViewContentModeScaleAspectFill;
  
}



@end



github主页:https://github.com/chenyufeng1991  。欢迎大家访问!

你可能感兴趣的:(iOS开发,iOS开发技术分享)