UIImageView

作者: vip4iPhonr | 来源:发表于2016-08-27 16:49 被阅读40次

一、UIImageView 基本属性

  • 1 创建UIImageView对象
UIImageView *imageView = [UIImageView alloc] init];
  • 2 设置尺寸
imageView.frame = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height);
  • 3 设置背景颜色
imageView.backgroundColor = [UIColor redColor];
  • 4 设置背景图片
imageView.image = [UIImage imageNamed:@"picture"];
:-> png格式不需要加后缀
  • 5 设置图片内容模式
imageView.contentMode = UIViewContentModeScaleAspectFill;
// 重新绘制 
UIViewContentModeRedraw, 
//1.带有Scale,标明图片有可能被完全的拉伸或压缩 
UIViewContentModeScaleToFill, 
//2.Aspect 比例,缩放是带有比例的 
UIViewContentModeScaleAspectFit, // 宽高比不变 Fit 适应 
UIViewContentModeScaleAspectFill, // 宽高比不变 Fill 填充
//3.不带有Scale,标明图片不可能被拉伸或压缩
UIViewContentModeCenter,
UIViewContentModeTop, 
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,

二、制作毛玻璃效果

1 创建UIToolBar 对象

UIToolBar *toolBar = [[UIToolBar alloc] init];

2 设置toolBar的frame

toolBar.frame = imageView.bounds;

3 设置毛玻璃的样式

toolBar.barStyle = UIBarStyleBlack;
toolBar.alpha = 0.95;

4 加载到父控制器(这里是imageView)中

[self.image addSubview:toolBar];

三、图片拉伸问题

  • 1 创建UIImage对象
UIImage *image = [UIImage imageNamed:@"image_name"];
  • 2 获取image的尺寸
CGFloat imagWidth = image.size.width;
CGFloat imageHeight = image.size.height;
  • 3 返回一张受保护而且拉伸的图片
  • 方式一
>>3.1
UIImage *resizableImage = [image resizableImageWithCapInsets:UIEdgeInsetMake(imageHeight * 0.5, imageWidth * 0.5, imageHeight * 0.5 -1, imageWidth * 0.5 - 1)];
>>3.2
UIImage *resizableImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(imageHeight * 0.5, imageWidth * 0.5, imageHeight * 0.5 -1, imageWidth * 0.5 - 1) resizingMode:UIImageResizingModeTile];
//UIImageResizingModeTile, 平铺(常用)
//UIImageResizingModeStretch, 拉伸(伸缩)
  • 方式二
//右边需要保护的区域 = 图片的width - leftCapWidth - 1
// bottom cap =  height - topCapHeight - 1
UIImage *resizableImage = [image stretchableImageWithLeftCapWidth:imageWidth * 0.5 topCapHeight:imageHeight * 0.5];

四、图片资源存放问题

  • 1 方式一
self.imageView.image = [UIImage imageNamed:@"1"];
  • 2 方式二
NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"];
self.imageView.image = [UIImage imageWithContentsOfFile:path];
  • 3 总结
加载图片有两种方式:

1.通过 imageNamed:来加载
2.通过 imageWithContentsOfFile:来加载

3.1 放到Assets.xcassets这里面的图片:
1> 打包后变成Assets.car
2> 拿不到路径
3> 只能通过imageNamed:来加载图片
4> 不能通过imageWithContentsOfFile:来加载图片
3.2 放到项目中的图片:
1> 可以拿到路径
2> 能通过imageNamed:来加载图片
3> 也能通过imageWithContentsOfFile:来加载图片
注意:

1>经常用的图片建议用imageNamed:
2>不经常用的通过imageWithContentsOfFile:来加载图片

相关文章

网友评论

    本文标题:UIImageView

    本文链接:https://www.haomeiwen.com/subject/kaqtettx.html