UIImageView的相关知识点

2018-09-24  本文已影响11人  打碟的DJ

  最近打算重新把OC的基本知识重新过一下,以后在学习的过程中会记录一些相关的知识点,一下是最近学习过程中对于UIImageView的相关知识的记录。

UIImageView的contentModel:

UIImageView *imageView = [[UIImageView alloc] init];
imageView.backgroundColor = [UIColor greenColor];
imageView.frame = CGRectMake(100, 100, 200, 100);
[self.view addSubview:imageView];
/*
 带有scale的,图片有可能会被拉伸
 UIViewContentModeScaleToFill, //将图片拉伸填充满整个imageView,图片显示的尺寸和imageView的尺寸是一样的
 
 //带有Aspect,保持图片的宽高比拉伸
 UIViewContentModeScaleAspectFit, //保证能看到图片的全部,保持宽高比拉伸显示在imageView居中显示
 UIViewContentModeScaleAspectFill, //等比例宽高拉伸,拉伸至宽/高和imageView一样显示,显示不下的部分会被裁剪掉
 
 //不带有Scale的:显示出来的图片不会被拉伸,保持图片原来的宽度和高度
 UIViewContentModeRedraw, //自定义视图并在缩放和计算操作期间重绘它们,不建议使用
 UIViewContentModeCenter,
 UIViewContentModeTop,
 UIViewContentModeBottom,
 UIViewContentModeLeft,
 UIViewContentModeRight,
 UIViewContentModeTopLeft,
 UIViewContentModeTopRight,
 UIViewContentModeBottomLeft,
 UIViewContentModeBottomRight,
 */
imageView.contentMode = UIViewContentModeScaleAspectFill;
//超出边框的内容会被裁剪掉
imageView.clipsToBounds = YES;
imageView.image = [UIImage imageNamed:@"UIImageView.jpg"];

UIImageView的animationImages(逐帧动画):

NSMutableArray *images = @[].mutableCopy;
//创建图片
for (int i = 0; i < 1278; i++) {
    NSString *imageName = [NSString stringWithFormat:@"Ex Iori Yagami_%04d",i];
    UIImage *image = [UIImage imageNamed:imageName];
    [images addObject:image];
}
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.animationImages = images;
self.imageView.animationRepeatCount = 1;  //执行次数,默认为0,为0时重复执行
[self.imageView startAnimating]; //开始动画
//    [self.imageView stopAnimating];  //结束动画

图片的缓存问题:

/*
 使用此方法,会自动缓存
 使用场景:图片较小,使用频率较高
 建议:把需要缓存的图片放到images.xcassets,或默认缓存
 */
UIImage *image = [UIImage imageNamed:@"111"];

/*
 只要方法名称有fiel的,都是传全路径
 使用场合:图片较大,使用频率较低
 建议:不能放在iamges.xcassets
 */
NSString *imageName = [[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"png"];
UIImage *image1 = [UIImage imageWithContentsOfFile:imageName];
上一篇 下一篇

猜你喜欢

热点阅读