- 测试环境:iPhone5C IOS8.3
- 测试代码:
NSMutableArray *arr1 = [NSMutableArray arrayWithCapacity:4];
NSMutableArray *arr2 = [NSMutableArray arrayWithCapacity:4];
NSMutableArray *arr3 = [NSMutableArray arrayWithCapacity:4];
for (int i = 0; i < 10; i++) {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:imgView];
// 1
uint64_t sTime = mach_absolute_time();
UIImage *img = [UIImage imageNamed:@"yulian_launch_0"];
uint64_t eTime = mach_absolute_time();
uint64_t elapsed = eTime - sTime;
NSLog(@"1.%lld", elapsed);
[arr1 addObject:@(elapsed)];
UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 250, 100, 100)];
[self.view addSubview:imgView1];
// 2
NSString *path = [[NSBundle mainBundle] pathForResource:@"yulian_launch_1" ofType:@"png"];
sTime = mach_absolute_time();
UIImage *img1 = [[UIImage alloc] initWithContentsOfFile:path];
eTime = mach_absolute_time();
elapsed = eTime - sTime;
NSLog(@"2.%lld", elapsed);
[arr2 addObject:@(elapsed)];
UIImageView *imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 400, 100, 100)];
[self.view addSubview:imgView2];
// 3
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"yulian_launch_2" ofType:@"png"];
sTime = mach_absolute_time();
NSData *imageD = [NSData dataWithContentsOfFile:path1];
UIImage *img2 = [UIImage imageWithData:imageD];
eTime = mach_absolute_time();
elapsed = eTime - sTime;
NSLog(@"3.%lld", elapsed);
[arr3 addObject:@(elapsed)];
}
- 执行耗时,单位纳秒
- 折线图可以直观的看出来,执行时间的差别,后两者是差不多,非缓存的执行时间大约是imageName的6倍(除首次)
imageName个人建议还是小图多次使用的场景使用。
其他两个大图或使用较少使用。