【发布时间】:2012-06-26 02:41:36
【问题描述】:
我在一个演示项目中使用 QuartzImage 类,我想要实现的是一个简单的帧显示单元,它基本上每 1/10 秒绘制一个图像 (320x480)。所以我的“帧速率”应该是每秒 10 帧。
在 QuartzImage 演示类中,有一个 drawInContext 方法,在这个方法中,它基本上是使用 CGContextDrawImage() 绘制一个 CGImageRef,我测量了完成完成所需的时间,平均大约需要 200 毫秒。
2011-03-24 11:12:33.350 QuartzDemo[3159:207] drawInContext took 0.19105 secs
-(void)drawInContext:(CGContextRef)context
{
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
CGRect imageRect;
imageRect.origin = CGPointMake(0.0, 0.0);
imageRect.size = CGSizeMake(320.0f, 480.0f);
CGContextDrawImage(context, imageRect, image);
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
NSLog(@"drawInContext took %2.5f secs", end - start);
}
谁能解释为什么要花这么长时间以及是否有其他方法可以提高性能? 200 毫秒似乎比应该花费的时间长得多。
更新 我尝试了@Brad-Larson 的建议,但没有看到很多性能改进。
所以更新的版本是我有自己的课
@interface FDisplay : UIView {
CALayer *imgFrame;
NSInteger frameNum;
}
end
所以在我的类实现中
- (id)initWithFrame:(CGRect)frame {
............
frameNum = 0;
NSString *file = [NSString stringWithFormat:@"frame%d",frameNum];
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:file ofType:@"jpg"]];
imgFrame = [CALayer layer];
CGFloat nativeWidth = CGImageGetWidth(img.CGImage);
CGFloat nativeHeight = CGImageGetHeight(img.CGImage);
CGRect startFrame = CGRectMake(0.0, 0.0, nativeWidth, nativeHeight);
imgFrame.contents = (id)img.CGImage;
imgFrame.frame = startFrame;
CALayer *l = [self layer];
[l addSublayer:imgFrame];
}
我有一个 NSTimer 在 0.1f 调用我的刷新方法
NSString *file = [NSString stringWithFormat:@"frame%d",frameNum];
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:file ofType:@"jpg"]];
frameNum++;
if (frameNum>100)
frameNum = 0;
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
[imgFrame setContents:(id)img.CGImage];
[CATransaction commit];
end = CFAbsoluteTimeGetCurrent();
NSLog(@"(%d)refresh took %2.5f secs", [[self subviews] count],end - start);
我认为我做的一切都是正确的,但帧速率仍然很低,
refresh took 0.15960 secs
【问题讨论】:
标签: iphone objective-c ios4 core-animation quartz-graphics