您的位置:首页 > 产品设计 > UI/UE

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:

指向它的指针被销毁,该资源会被干掉

放到项目中的图片就不带有缓存

适用范围不经常使用的图片,大批量的图片
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: