UIImageView 的基本使用

创建UIImageView对象

UIImageView*imageView = [[UIImageView alloc]init];

设置frame

imageView.frame=CGRectMake(100,100,250,200);

设置背景

imageView.backgroundColor = [UIColor greenColor];

设置图片(png不需要后缀)

imageView.image= [UIImage imageNamed:@"1"];

UIViewContentModeRedraw, //重新绘制(核心绘图) drawRact

//带有Scale,标明图片有可能被拉伸或压缩
 UIViewContentModeScaleToFill, //完全的压缩或拉伸

 // Aspect比例,缩放是带有比例的
 UIViewContentModeScaleAspectFit, //宽高比不变Fit适应
 UIViewContentModeScaleAspectFill, //宽高比不变Fill填充

 //不带有Scale,标明图片不可能被拉伸或压缩
 UIViewContentModeCenter,
 UIViewContentModeTop,
 UIViewContentModeBottom,
 UIViewContentModeLeft,
 UIViewContentModeRight,
 UIViewContentModeTopLeft,
 UIViewContentModeTopRight,
 UIViewContentModeBottomLeft,
 UIViewContentModeBottomRight,
 */

设置图片的内容模式

imageView.contentMode=UIViewContentModeScaleAspectFill;

加到控制器的view中

[self.viewaddSubview:imageView];

裁剪多余的部分

imageView.clipsToBounds=YES;


UIimageView 的 frame 设置

设置frame的方式

  • 方式一

UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"1"];
imageView.frame = CGRectMake(100, 100, 267, 400);
imageView.frame = (CGRect){{100, 100},{267, 400}};
  • 方式二

UIImageView *imageView = [[UIImageView alloc] init];

//创建一个UIImage对象

UIImage *image = [UIImage imageNamed:@"1"];

//设置frame

imageView.frame = CGRectMake(100, 10, image.size.width, image.size.height); 

//设置图片

imageView.image = image;
  • 方式三

//创建一个UIImage对象
 UIImage *image = [UIImage imageNamed:@"1"];
 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 10, image.size.width, image.size.height)];
 imageView.image = image;
  • 方式四

// 创建一个UIimageview对象
    // 注意: initWithImage 默认就有尺寸--->图片的尺寸
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];

    // 改变位置
   // imageView.center = CGPointMake(200, 150);

    imageView.center = CGPointMake(self.view.frame.size.width * 0.5, self.view.frame.size.height * 0.5);

results matching ""

    No results matching ""