美文网首页
3、UIImageView使用

3、UIImageView使用

作者: 程序萌 | 来源:发表于2020-03-10 15:41 被阅读0次

UIImageView,是一个图片的展示框,默认是没有用户交互功能的,如果需要有交互功能,需要自己手动打开 TA 的交互功能。

  • 初始化imageView
UIImageView *imageView = [[UIImageView alloc] init];
  • 设置位置
imageView.frame = CGRectMake(101, 200, 173, 173)];
  • 初始化 image 设置要显示的图片名称

1、第一张创建方式 系统会把图像Cache到内存。如果图像比较大,或者图像比较多,用这种方式会消耗很大的内存
2、通过imageNamed: 这种方式加载图片,加载好的图片会一直保存写在内存中,不会释放,这样下次如果再使用同样的图片的时候就不需要再重新加载了,因为内存里面已经有了。缺点就是: 如果加载了大量的图片,那么这些图片会一直保留在内存中,导致应用程序占用内存过大(这就叫缓存)
3、使用这种方式加载图片,加载图片即便没有强类型指针引用也不会别销毁(会被缓存)

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

1、第二种创建方式 保存路径而不是保存图片这样就能减少内存了 不会使用缓存
2、使用下面这种方式加载的图片,只要没有强类型指针引用就会被销毁了
3、这里的参数不能再传递图片了,这里需要传递一个图片的完整路径

 NSString *path = [[NSBundle mainBundle] pathForResource:@"image_photo" ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile: path];
  • 初始化2种方法的区别
 // initWithImage:会根据传入的图片创建imageView, 如果不设置frame,可以显示出来图片,因为系统会默认设置坐标为 0,0,宽高等于图片的宽高
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2"]];

// 这种方法创建的imageView,如果不设置frame,会显示不出来图片,因为oc语法规定:不能直接修改oc对象结构体属性的成员
/* 
CGRect 是结构体
struct CGRect {
    CGPoint origin;
    CGSize size;
};
*/
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"2"];
  • 把图片添加到imageView上
imageView.image = image;
  • 设置圆角
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = 10;
  • 设置边框的颜色和大小
imageView.layer.borderColor = [UIColor blueColor].CGColor;
imageView.layer.borderWidth = 2;
  • 隐藏或者显示图片 默认是NO
imageView.hidden = YES;
  • 设置透明度
imageView.alpha =0.5; 
  • 设置高亮时显示的图片,图片需要自己创建
 UIImage *himage = [UIImage imageNamed:@"like.png"];
 imageView.highlightedImage = himage;
 [imageView setHighlighted:YES];
  • 为iamgeView开启交互功能
// 为iamgeView开启交互功能
imageView.userInteractionEnabled = YES;
  • 设置点击事件
// 设置点击事件
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)];
// 为imageView添加点击事件
[imageView addGestureRecognizer:tapGestureRecognizer];

// 点击事件方法实现,可以选择是否带参数
- (void)click { 
  NSLog(@"我被点击了!");
}
  • UIImageView还有一个属性(contentMode),是当图片大小小于UIImageVIew时处理图片的。
// contentMode是用来设置图片的显示方式,TA很多的常量
//如果图片视图和图片大小不一样,图片默认填满视图
/*
UIViewContentModeScaleToFill,      默认模式,填满,这种图片会变形
 UIViewContentModeScaleAspectFit,   按比例填充,这种不会变形,但是图片会变小
 UIViewContentModeScaleAspectFill,  按比例填满,这种不会变形,但是图片有可能超出图片视图边界
 UIViewContentModeRedraw,           和第一个一样
 UIViewContentModeCenter        保持图片不变

//当图片小于图片视图的时候,图片的停靠方式
 UIViewContentModeTop,
 UIViewContentModeBottom,
 UIViewContentModeLeft,
 UIViewContentModeRight,
 UIViewContentModeTopLeft,
 UIViewContentModeTopRight,
 UIViewContentModeBottomLeft,
 UIViewContentModeBottomRight,
*/
// 这里我们就选择第一个设置
imageView.contentMode = UIViewContentModeScaleAspectFit;
  • 超出imageView的范围会被裁减掉 默认是可以超出的 NO
imagView.clipsToBounds = YES;
  • 让UIImageView从一个相册变成一个“动态图”。
 NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:0];
    for (int i = 1; i < 30; i++) {
        // 获取图片名称,如果png结尾的可以不加上后缀,不然一定要加上后缀,不加后缀系统读取不到图片的
        NSString *picStr = [NSString stringWithFormat:@"test%d", i];
        // 获取每一张图片对象
        UIImage *img = [UIImage imageNamed:picStr];
        [mArr addObject:img];
    }
    
    // 指定做动画的所有图片
    imageView.animationImages = mArr;
 
    // 指定动画时间,动画重复次数
    imageView.animationDuration = 0.3;
    imageView.animationRepeatCount = 0;
    
    // 开启动画
    [imageView startAnimating];

相关文章

网友评论

      本文标题:3、UIImageView使用

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