Iphone 使用实时预览的核心图形绘制矩形
我正在创建一个简单的绘图应用程序,希望获得一些基于用户触摸事件绘制矩形的帮助。我能够使用核心图形从触摸开始点到触摸结束点绘制一个矩形。触发touchesEnded事件后,将显示此矩形 然而,我希望能够以“现场”的方式做到这一点。这意味着,当用户拖动手指时,矩形将更新并显示。这可能吗?任何帮助都将不胜感激,谢谢 更新: 我能够通过使用以下代码来实现这一点。对于小图像,它工作得非常好,但是对于大图像,它的速度非常慢。我意识到我的解决方案效率极低,我想知道是否有人能提出更好的解决方案。谢谢Iphone 使用实时预览的核心图形绘制矩形,iphone,core-graphics,Iphone,Core Graphics,我正在创建一个简单的绘图应用程序,希望获得一些基于用户触摸事件绘制矩形的帮助。我能够使用核心图形从触摸开始点到触摸结束点绘制一个矩形。触发touchesEnded事件后,将显示此矩形 然而,我希望能够以“现场”的方式做到这一点。这意味着,当用户拖动手指时,矩形将更新并显示。这可能吗?任何帮助都将不胜感激,谢谢 更新: 我能够通过使用以下代码来实现这一点。对于小图像,它工作得非常好,但是对于大图像,它的速度非常慢。我意识到我的解决方案效率极低,我想知道是否有人能提出更好的解决方案。谢谢 -
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
_firstPoint = [touch locationInView:self.view];
_firstPoint.y -= 40;
_lastPoint = _firstPoint;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 40;
[self drawSquareFrom:_firstPoint to:currentPoint];
_lastPoint = currentPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 40;
[self drawSquareFrom:_firstPoint to:currentPoint];
[_modifiedImage release];
_modifiedImage = [[UIImage alloc] initWithCGImage:_imageView.image.CGImage];
}
- (void)drawSquareFrom:(CGPoint)firstPoint to:(CGPoint)lastPoint
{
_imageView.image = _modifiedImage;
CGFloat width = lastPoint.x - firstPoint.x;
CGFloat height = lastPoint.y - firstPoint.y;
_path = [UIBezierPath bezierPathWithRect:CGRectMake(firstPoint.x, firstPoint.y, width, height)];
UIGraphicsBeginImageContext( _originalImage.size );
[_imageView.image drawInRect:CGRectMake(0, 0, _originalImage.size.width, _originalImage.size.height)];
_path.lineWidth = 10;
[[UIColor redColor] setStroke];
[[UIColor clearColor] setFill];
[_path fill];
[_path stroke];
_imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
您现在正在做的是为对
drawSquareFrom:to:
的每次调用创建一个成对的CGImage
和cgitmapcontext
,并每次处理它们。相反,创建一个成对的CGImage
和cgitmapcontext
,供每次调用重用
-(void) drawSquareFrom:(CGPoint)from to:(CGPoint)to {
CGContextRef context = [self offscreenContext];
CGRect draw , full = [self offscreenContextBounds];
draw.origin = from;
draw.size.width = to.x - from.x;
draw.size.height = to.y - from.y;
draw = CGRectStandardize( draw );
[[UIColor redColor] setStroke];
[[UIColor clearColor] setFill];
CGContextClearRect( context , full );
CGContextFillRect( context , draw );
CGContextStrokeRectWithWidth( context , draw , 10 );
[_imageView setNeedsDisplay];
}
您可以像这样创建一对CGImage和CGContext,尽管有一些。。。填补。所有myX变量都是类成员
-(CGContextRef) offscreenContext {
if ( nil == myContext ) {
size_t width = 400;
size_t height = 300;
CFMutableDataRef data = CFDataCreateMutable( NULL , width * height * 4 ); // 4 is bytes per pixel
CGDataProviderRef provider = CGDataProviderCreateWithCFData( data );
CGImageRef image = CGImageCreate( width , height , ... , provider , ... );
CGBitmapContextRef context = CGBitmapContextCreate( CFDataGetMutableBytePtr( data ) , width , height , ... );
CFRelease( data ); // retained by provider I think
CGDataProviderRelease( provider ); // retained by image
myImage = image;
myContext = context;
myContextFrame.origin = CGPointZero;
myContextFrame.size.width = width;
myContextFrame.size.height = height;
_imageView.image = [UIImage imageWithCGImage:image];
}
return myContext;
}
-(CGRect) offscreenContextBounds {
return myContextFrame;
}
这会将_imageView.image设置为新创建的映像。在drawSquareFrom:to:
中,我假设setNeedsDisplay足以绘制所做的更改,但每次包装相同的CGImage时,可能需要分配一个新的UIImage。只需使用两个上下文即可。
在其中,你只需“预览”绘制矩形,你可以在触摸移动时清除它。
在触摸屏上,您最终在原始上下文上绘制,然后在图像上绘制。与其绘制位图图像,不如创建由CALayer对象组成的图像?(即Illustrator vs Photoshop)在这种情况下,您可以轻松地将带有矩形路径的CAShapeLayer临时添加到文档中。。用户完成拖动后将其提交到文档。在任何情况下,您都可以使用CAShapeLayer预览您的矩形,该CAShapeLayer在文档视图中的当前图像上具有矩形路径。