- (void)viewDidLoad {
[super viewDidLoad];
// 想要加载一张图片,UIImgae 和 UIImage 的一个载体 UIImageView
// 1.初始化UIImage对象
//iamgeNamed: 会认为是一个png。不用写后缀名
UIImage *image1 = [UIImage imageNamed:@"weibo"];
// 沙盒
// 找到文件目录 图片后缀名要写
NSString *path = [NSString stringWithFormat:@"%@/Untitled-1.fw.png",[[NSBundle mainBundle] resourcePath]];
NSLog(@"%@",path);
// 2.初始化UIIImage对象
UIImage *image2 = [[UIImage alloc] initWithContentsOfFile:path];
/**
<1. 如果是用imageNamed方法获取的图片会被缓存在内存中(将本地文件读出缓存到内存中),下一次使用时直接从内存加载不需要再次读取图片,效率非常高,但是会占有一定的内存
多次调用的小图片会选用此方法加载
<2.initWithContentsOfFile方法是从本地或者网络读取图片文件,但是不加载到缓存中,我们可以手动释放掉,每次加载图片都要读取源文件,所以在读取上效率较低,但是我们可以随时操作内存的释放
当我们要加载高精度大图时,使用此方法
*/
// 载体 - 最基本单元UIImageView :UIView
UIImageView *imageView = [[UIImageView alloc] initWithImage:image1];
// 1.显示图片的大小不是图片本身决定,而是由imageView的frame决定
// imageView.frame = CGRectMake(10, 30, image1.size.width, image1.size.height);
// 设置中心点
// imageView.center = CGPointMake(320/2, 480/2);
imageView.frame = CGRectMake(10, 30, 300, 440);
imageView.backgroundColor = [UIColor greenColor];
// 内容模式 (UIView属性)
// imageView.contentMode = UIViewContentModeBottomRight; // 右下角
// UIViewContentModeScaleToFill默认状态,改变比例,拉伸充满整个载体
// UIViewContentModeScaleAspectFill 不改变比例缩放充满整个载体,但是由于载体可能和图片比例不同,所以会有超出的一边
// UIViewContentModeScaleAspectFit 不改变比例缩放充满载体的短边即可,由于载体可能跟图片比例不同,所有会有空余的地方
imageView.contentMode = UIViewContentModeScaleAspectFit;
// [self.view addSubview:imageView];
// UIImageView动画 (播放序列图)
// 1. 将序列图加入数组
NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
for (int i = 1; i <= 13; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"png%d.png",i]];
[imagesArray addObject:image];
}
UIImageView *imageView1 = [[UIImageView alloc] init];
imageView1.frame = CGRectMake(10, 200, 300, 217);
imageView1.backgroundColor = [UIColor whiteColor];
[self.view addSubview:imageView1];
// 设置序列图数组
// An array of UIImage objects to use for an animation.
// 一个UIImage对象数组 用作动画使用
imageView1.animationImages = imagesArray;
// 设置播放周期时间
imageView1.animationDuration = 1;
// 设置播放次数
imageView1.animationRepeatCount = 0; // 不限制
// 开始播放动画
[imageView1 startAnimating];
// 停止动画
// [imageView1 stopAnimating];
}
UIImageView和UIImage
最新推荐文章于 2018-11-02 14:57:28 发布