ios - UIImage (UIGraphicsBeginImageContextWithOptions()) 创建速度比使用 PNG 初始化更快吗?

标签 ios objective-c uiimage png

使用此类方法并创建纯色 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/

相关文章:

android - 如何从 iOS 和 Android 上的 PhoneGap 搜索 native Google map 应用程序中的文本字符串?

iphone - 如何在 UIImage 中只显示图像的一部分?

ios - 如何在应用程序中将图像保存在本地

ios - 使用 MagicalRecord 在后台执行获取请求

ios - 为什么我的格式化程序返回一个 nil NSDate

objective-c - 如何从 View Controller 中 resignFirstResponder

ios - 在 View 之间传递图像

iphone - 以编程方式全局IP地址

iphone - 绘制 UIImage 的选定区域

ios - 将 NSString 转换为 NSUTF32StringEncoding