我最近下载了GLPaint示例代码,并查看了其中非常有趣的部分。有一个recordedPaths NSMutableArray,它包含一些点,然后由GLPaint读取和绘制。
它在这里声明:
NSMutableArray *recordedPaths;
recordedPaths = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Recording" ofType:@"data"]];
if([recordedPaths count])
[self performSelector:@selector(playback:) withObject:recordedPaths afterDelay:0.2];
播放代码如下:
- (void) playback:(NSMutableArray*)recordedPaths {
NSData* data = [recordedPaths objectAtIndex:0];
CGPoint* point = (CGPoint*)[data bytes];
NSUInteger count = [data length] / sizeof(CGPoint),
i;
//Render the current path
for(i = 0; i < count - 1; ++i, ++point)
[self renderLineFromPoint:*point toPoint:*(point + 1)];
//Render the next path after a short delay
[recordedPaths removeObjectAtIndex:0];
if([recordedPaths count])
[self performSelector:@selector(playback:) withObject:recordedPaths afterDelay:0.01];
}
从这里我了解到recordedPaths是一个可变数组,它构造了CGPoint的c数组,然后读取并呈现。我想放入我自己的数组中,但我一直在这方面遇到麻烦。
我尝试将recordedPaths声明更改为:
NSMutableArray *myArray = [[NSMutableArray alloc] init];
CGPoint* points;
CGPoint a = CGPointMake(50,50);
int i;
for (i=0; i<100; i++,points++) {
a = CGPointMake(i,i);
points = &a;
}
NSData *data = [NSData dataWithBytes:&points length:sizeof(*points)];
[myArray addObject:data];
不过这不管用。有什么建议吗?
发布于 2010-12-09 16:20:20
如果您查看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。
发布于 2010-02-28 06:50:44
CGPoint* points;
CGPoint a = CGPointMake(50,50);
int i;
for (i=0; i<100; i++,points++) {
a = CGPointMake(i,i);
points = &a;
}
NSData *data = [NSData dataWithBytes:&points length:sizeof(*points)];
错误的代码。
(1)你需要一个点数组。简单地声明CGPoint* points;
不会创建一个点数组,只会创建一个未初始化的CGPoint指针。您需要为阵列分配空间,可以使用
CGPoint points[100];
或
CGPoint* points = malloc(sizeof(CGPoint)*100);
如果您选择malloc
方式,请记住要free
这些点。
(2)将值复制到需要使用的指针内容的
*points = a;
但我建议你在循环中保持指针points
不变,因为你以后会重用它。使用数组语法points[i]
。
(3)
sizeof(*points)
因为*points
只有一个CGPoint,所以sizeof总是8个字节。您需要将结果乘以100才能得到正确的长度。
(4)
[NSData dataWithBytes:&points ...
points
已经是指向实际数据的指针。你不需要再记一遍它的地址。直接传递points
即可。
因此,最终的代码应该如下所示
CGPoint* points = malloc(sizeof(CGPoint)*100); // make a cast if the compiler warns.
CGPoint a;
int i;
for (i=0; i<100; i++) {
a = CGPointMake(i,i);
points[i] = a;
}
NSData *data = [NSData dataWithBytes:points length:sizeof(*points)*100];
free(points);
发布于 2010-04-08 22:44:07
我只是在阅读这篇文章,因为我正在努力实现类似的事情。通过Apple对原始项目的修改,我能够通过相应地修改代码来创建一个新的“形状”。
注意,它只画了一条对角线...好几次了。但这个理论是为了创建你自己的图画。
我已经从KennyTM的帖子中获取了代码,并将其合并到'payback‘函数中,这可以调整为在initWithCoder函数中创建数组,然后像原始代码一样发送它,但现在-这将得到结果。
CGPoint* points = malloc(sizeof(CGPoint)*100);
CGPoint a;
int iter;
for (iter=0; iter<200; iter++) {
a = CGPointMake(iter,iter);
points[iter] = a;
}
NSData *data = [NSData dataWithBytes:points length:sizeof(*points)*100];
free(points);
CGPoint* point = (CGPoint*)[data bytes];
NSUInteger count = [data length] / sizeof(CGPoint),
i;
for(i = 0; i < count - 1; ++i, ++point)
[self renderLineFromPoint:*point toPoint:*(point + 1)];
[recordedPaths removeObjectAtIndex:0];
if([recordedPaths count])
[self performSelector:@selector(playback:) withObject:recordedPaths afterDelay:0.01];
我还处于openGL编程的第一个星期,所以请原谅任何明显的错误/糟糕的方法,并感谢您的帮助!
希望这能有所帮助
https://stackoverflow.com/questions/2350168
复制相似问题