Reputation: 1037
Would using this class method and creating a solid color UIImage be faster than creating a png with a solid color?
+ (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;
}
Thoughts?
Upvotes: 0
Views: 545
Reputation: 63667
Hitting the disk and reading from cache (imageNamed:
) is faster than 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:
seems to be faster both on iPhone and the simulator.
2013-05-09 09:47:22.844 Graph[8032:c07] 7 ms
2013-05-09 09:47:22.948 Graph[8032:c07] 101 ms
Upvotes: 1
Reputation: 9944
I don't think it'd make a huge difference. I'd say loading from a PNG is faster. This post have a comparison.
The big advantage of creating the image via code is that you can easily change the color, not needing to generate another image resource and adding it to the project.
Upvotes: 0