iOS - UIImageView常见使用方法和多图动画播放
2018-03-01 00:40
513 查看
UIImageView
显示图片
// 创建对象 UIImageView *imageView = [[UIImageView alloc] init]; // frame imageView.frame = self.view.bounds; // 设置背景 imageView.backgroundColor = [UIColor greenColor]; // 设置图片 imageView.image = [UIImage imageNamed:@"1"]; /* // 带有Scale图片可能被拉伸 UIViewContentModeScaleToFill, // Aspect比例,缩放是带比例的 UIViewContentModeScaleAspectFit, UIViewContentModeScaleAspectFill, // 不会被拉伸 UIViewContentModeRedraw, UIViewContentModeCenter, UIViewContentModeTop, UIViewContentModeBottom, UIViewContentModeLeft, UIViewContentModeRight, UIViewContentModeTopLeft, UIViewContentModeTopRight, UIViewContentModeBottomLeft, UIViewContentModeBottomRight,*/ // 设置图片内容模式 imageView.contentMode = UIViewContentModeScaleAspectFit; // 是否裁多余的 imageView.clipsToBounds = YES; // 创建毛玻璃 UIToolbar *toolbar = [[UIToolbar alloc] init]; // 设置毛玻璃尺寸 toolbar.frame = imageView.bounds; // 设置毛玻璃的样式 toolbar.barStyle = UIBarStyleBlack; toolbar.alpha = 0.9; [imageView addSubview:toolbar]; // 加载到控制器view中 [self.view addSubview:imageView];
frame
第一种和第二种
// 创建对象 // UIImageView *imageView = [[UIImageView alloc] init]; // imageView.image = [UIImage imageNamed:@"1"]; // frame设置方式 // 第一种 // imageView.frame = CGRectMake(0, 0, 500, 833); // 第二种 UIImage *image = [UIImage imageNamed:@"1"]; imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height); imageView.image = image;
第三种
// 第三种 UIImage *image = [UIImage imageNamed:@"1"]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)]; imageView.image = image;
第四种
// 第四种 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
// 修改位置 imageView.center = CGPointMake(10, 10); // 加载到控制器view中 [self.view addSubview:imageView];
取图片
// 加载图片 // 方式一 // self.imageView.image = [UIImage imageNamed:@"1"]; // 方式二 NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"]; self.imageView.image = [UIImage imageWithContentsOfFile:path]; // 放入Assets.xcassets中的路径,取不到,不能使用imageWithContentsOfFile: // 只能使用imageNamed: // 放入项目中的图片两中都可以取到
多图片组成动画
1.加载资源
如果建立真实文件夹,在调用文件夹中的东西的时候需要将路径全部写出,如果是虚拟文件夹则不用
2.抽取到的加载图片的方法
- (NSArray *)loadAllImageWithimagePrefix:(NSString *)imagePrefix count:(int)count{ NSMutableArray<UIImage *> *images = [NSMutableArray array]; for (int i = 0; i < count; i++) { // 获取图片名称 NSString *imageName = [NSString stringWithFormat:@"%@_%d", imagePrefix, i + 1]; // 以名称创建UIImage UIImage *image = [UIImage imageNamed:imageName]; // 装入数组 [images addObject:image]; } // 返回图片数组 return images; }
- (void)viewDidLoad { [super viewDidLoad]; self.useImage = [self loadAllImageWithimagePrefix:@"Prefix" count:250]; }
抽取对动画播放的方法
- (void)playAnimationWithImagesArray:(NSArray *)imagesArray repeatCount:(int)count duration:(float)duration{ // 设置播放动画图片 self.imageView.animationImages = imagesArray; // 设置播放次数 0就是不限次 self.imageView.animationRepeatCount = count; // 播放时长 self.imageView.animationDuration = duration; // 播放 [self.imageView startAnimating]; }
// 延迟实行方法 [self performSelector:@selector(fun) withObject:nil afterDelay:self.imageView.animationDuration];
图片的两种加载方式
imageNamed:
指向它的指针被销毁,该资源也不会干掉放到Assets.xcassets的图片,默认就有缓存
适用范围图片经常使用
ImageWithContentsOfFile:
指向它的指针被销毁,该资源会被干掉放到项目中的图片就不带有缓存
适用范围不经常使用的图片,大批量的图片
相关文章推荐
- iOS控件——UIView与UIImageView播放动画的实现方法
- IOS使用UIImageView显示gif动画的例子
- iOS--UIImageView的使用方法详细
- 使用MFMailComposeViewController在iOS应用内编辑邮件使用方法及常见问题
- IOS使用UIImageView显示gif动画的例子
- IOS中UIImageView方法实现简单动画
- IOS使用UIImageView显示gif动画的例子
- 【iOS开发】关于显示一连串图片组成动画效果UIImageView的使用
- IOS UI学习教程之使用UIImageView控件制作动画
- IOS学习笔记(十)之UIImageView图片视图的基本概念和使用方法
- IOS--UIImageView的使用方法
- iOS开发>>>UIImageView连续播放图片,组成动画
- [置顶]iOS--使用UIImageView进行GIF动图播放
- iOS --- 使用UIImageView来实现倒计时动画
- 使用UIImageView播放动画
- ios UIImageView添加图片动画效果,添加了手势之后,依然没有反应的解决方法
- iPhone 如何使用UIImageView播放动画,并停留在之后一张图片并添加播放结束时的事件
- IOS开发-UI学习-使用UIImageView控件制作动画
- IOS使用UIImageView显示gif动画的例子
- IOS学习笔记(十)之UIImageView图片视图的基本概念和使用方法