UIImageView基本使用 / UIImageView播放序列帧动画
1. UIImageView基本使用
/*
注意:
如果是通过[[UIImageView alloc] init];创建的图片, 没有默认的宽高
但是如果是通过[[UIImageView alloc] initWithImage:image];创建的图片, 有默认的宽高
默认的宽高就是图片的宽高
修改控件的frame的注意点:
// 注意: OC语法规定, 不能直接修改一个"对象"的"结构体属性"的"成员"
// 如果不能直接修改一个"对象"的"结构体属性"的"成员", 那么如果真的想改怎么办?
// iv.frame.size = image.size; // 错误
/* 正确做法:先取出 --> 再修改 --> 重新赋值 */
iv.frame = CGRectMake(0, 0, image.size.width, image.size.height);
// iv.frame = (CGRect){{0, 0}, {image.size.width, image.size.height}};
// 一个{}对应一个结构体
iv.frame = (CGRect){{0, 0}, {image.size.width, image.size.height}};
iv.image = [UIImage imageNamed:@"meinv.jpg"];
iv.contentMode = UIViewContentModeScaleAspectFill;
// 剪切超出的部分
iv.clipsToBounds = YES;
NSMutableArray *arrM = [NSMutableArray array];
// 1.创建图片
for (int i = 1; i <= 10; i++) {
NSString *imageNmae = [NSString stringWithFormat:@"stand_%i", i];
UIImage *image = [UIImage imageNamed:imageNmae];
// 2.将所有的图片放到数组中
[arrM addObject:image];
}
// 3.将保存了所有图片的数组赋值给UIImageView
self.containerView.animationImages = arrM;
self.containerView.animationRepeatCount = 1; // 设置重复次数
self.containerView.animationDuration = 1;
[self.containerView startAnimating]; // 开始动画
/* iv.contentMode = UIViewContentModeScaleAspectFill;
规律:
但凡取值中包含Scale单词的, 都会对图片进行拉伸(缩放)
但凡取值中没有出现Scale单词的, 都不会对图片进行拉伸
UIViewContentModeScaleToFill,
> 会按照UIImageView的宽高比来拉伸图片
> 直到让整个图片都填充UIImageView为止
> 因为是按照UIImageView的宽高比来拉伸, 所以图片会变形
规律:
但凡取值中包含Aspect单词的, 都会按照图片的宽高比来拉伸
> 因为是按照图片的宽高比来拉伸, 所以图片不会变形
UIViewContentModeScaleAspectFit,
> 会按照图片的宽高比来拉伸
> 要求整张图片都必须在UIImageView的范围内
> 并且宽度和高度其中一个必须和UIImageView一样
> 居中显示
UIViewContentModeScaleAspectFill,
> 会按照图片的宽高比来拉伸
> 要求整张图片必须填充UIImageView
> 并且图片的宽度或者高度其中一个必须和UIImageView一样
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
*/
2. UIImageView播放序列帧动画
- (void)palyAnimation:(NSString *)imageName imageCount:(NSInteger)count{
/********************** 执行指定动作动画一次 start ******************************/
// 1. 获取到执行动作的所有图片集合
NSMutableArray *imagesArray = [NSMutableArray array];
// 图片下标从 1 开始
for (int i = 1; i<=count; i++) {
NSString *name = [NSString stringWithFormat:@"%@_%i.png", imageName, i]; // stand_1.png
name = [[NSBundle mainBundle] pathForResource:name ofType:nil];
/*
UIImage *image = [UIImage imageNamed:name];使用UIImage的imageNamed:方法,会导致内存不断上升,因为该方法会不断地缓存图片,不会立即释放,而使用UIImage的imageWithContentsOfFile:图片会使用完毕后立即释放,而不会引发内存不断上升导致泄露问题
[UIImage imageNamed:name]会导致内存爆增, 并且动画执行完毕之后也没有下降的原因
imageNamed:这个方法会自动缓存创建的图片
如果图片不是经常使用, 并且图片非常占用资源, 那么不建议大家使用imageNamed方法加载图片
UIImage *image = [UIImage imageNamed:imageNmae];
[UIImage imageWithContentsOfFile:name], OfFile需要传递一个图片全路径
*/
UIImage *image = [UIImage imageWithContentsOfFile:name];
[imagesArray addObject:image];
}
// 2. 设置imageView的动画图片集
self.imageview.animationImages = imagesArray;
// 3. 执行动画操作
self.imageview.animationRepeatCount = 1; // 动画执行次数,若为0时循环执行将
self.imageview.animationDuration = imagesArray.count * 0.05; // 设置动画执行时间
[self.imageview startAnimating];
/********************** 执行指定动作动画一次 end ******************************/
// 如果不是站立,在执行上面动画后,延时执行站立动画
if (![@"stand" isEqualToString:imageName]) {
[self performSelector:@selector(stand) withObject:nil afterDelay:self.imageview.animationDuration inModes:nil];
}
}

浙公网安备 33010602011771号