开发者

Recording Touches on UIView

开发者 https://www.devze.com 2023-03-08 17:35 出处:网络
I\'ve got an app where I can draw onto some \'slides\' to make notes. What I want to be able to do is \'save\' these notes so they can be viewed at another time. Perhaps creating a file wit开发者_开发

I've got an app where I can draw onto some 'slides' to make notes. What I want to be able to do is 'save' these notes so they can be viewed at another time. Perhaps creating a file wit开发者_开发技巧h touch tracking information, which can then be 'loaded' at the users convenience.

I have the code for drawing on the screen as follows:

UIGraphicsBeginImageContext(self.view.frame.size);
[imageView.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

So how do I modify this code to allow touch tracking and thus saving to a file?

Cheers, Dan


Two ways to do this. You can either save the image. See docs on UIImageJPEGRepresentation() and UIImagePNGRepresentation() The other way is to save the touch locations.

ie.

NSMutableArray *array = [NSMutableArray array]; [array addObject:[NSValue valueForCGPoint:currentPoint]];

You can then save this array out with [array writeToFile:<valid file path> atomically:YES]. Finally to get the lines back reread the file and recreate the paths.

0

精彩评论

暂无评论...
验证码 换一张
取 消