我注意到默认情况下,示例应用程序GLPaint带有二进制记录数据,并在启动时加载,它用于显示“摇我”文本。
我正在创建一个类似的绘画应用程序,我只是想知道实际记录这些笔划的最佳方法是什么,并在下次加载它们。
目前我尝试使用Core Data保存每个顶点的位置,然后在开始时它会再次读取并渲染所有点,但是我发现它太慢了。
有更好/更有效的方法吗?可以将整个viewBuffer保存为二进制文件然后重新加载吗?
答案 0 :(得分:2)
如果查看Recording.data,您会注意到每一行都是它自己的数组。要捕获墨迹并播放它,您需要有一个数组数组。出于本演示的目的 - 声明一个可变数组 - writRay
@synthesize writRay;
//init in code
writRay = [[NSMutableArray alloc]init];
捕捉墨迹
// Handles the continuation of a touch.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGRect bounds = [self bounds];
UITouch* touch = [[event touchesForView:self] anyObject];
// Convert touch point from UIView referential to OpenGL one (upside-down flip)
if (firstTouch) {
firstTouch = NO;
previousLocation = [touch previousLocationInView:self];
previousLocation.y = bounds.size.height - previousLocation.y;
/******************* create a new array for this stroke's points **************/
[writRay addObject:[[NSMutableArray alloc]init]];
/***** add 1st point *********/
[[writRay objectAtIndex:[writRay count] -1]addObject:[NSValue valueWithCGPoint:previousLocation]];
} else {
location = [touch locationInView:self];
location.y = bounds.size.height - location.y;
previousLocation = [touch previousLocationInView:self];
previousLocation.y = bounds.size.height - previousLocation.y;
/********* add additional points *********/
[[writRay objectAtIndex:[writRay count] -1]addObject:[NSValue valueWithCGPoint:previousLocation]];
}
// Render the stroke
[self renderLineFromPoint:previousLocation toPoint:location];
}
播放墨水。
- (void)playRay{
if(writRay != NULL){
for(int l = 0; l < [writRay count]; l++){
//replays my writRay -1 because of location point
for(int p = 0; p < [[writRay objectAtIndex:l]count] -1; p ++){
[self renderLineFromPoint:[[[writRay objectAtIndex:l]objectAtIndex:p]CGPointValue] toPoint:[[[writRay objectAtIndex:l]objectAtIndex:p + 1]CGPointValue]];
}
}
}
}
为获得最佳效果,请摇动屏幕以清除并从AppController中的changeBrushColor调用playRay。