使用此类方法并创建纯色 UIImage 会比创建纯色 png 更快吗?
+ (UIImage*) imageWithColor:(UIColor*)color size:(CGSize)size
{
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
UIBezierPath* rPath = [UIBezierPath bezierPathWithRect:CGRectMake(0., 0., size.width, size.height)];
[color setFill];
[rPath fill];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
想法?
最佳答案
访问磁盘并从缓存中读取 (imageNamed:
) 比 Quartz 更快。
-(void) testLoadVSCreate
{
[self testBlock:^{
[UIImage imageNamed:@"100x100.png"];
} times:1000];
[self testBlock:^{
[[self class] imageWithColor:[UIColor redColor] size:CGSizeMake(100, 100)];
} times:1000];
}
-(void) testBlock:(void (^)(void)) block times:(NSUInteger)times {
double a = CFAbsoluteTimeGetCurrent();
while (times--) block();
double b = CFAbsoluteTimeGetCurrent();
unsigned int m = ((b-a) * 1000.0f);
NSLog(@"%d ms", m);
}
imageNamed:
似乎在 iPhone 和模拟器上都更快。
2013-05-09 09:47:22.844 Graph[8032:c07] 7 ms
2013-05-09 09:47:22.948 Graph[8032:c07] 101 ms
关于ios - UIImage (UIGraphicsBeginImageContextWithOptions()) 创建速度比使用 PNG 初始化更快吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16452819/