将CGImage绘制到Bitmap上下文中,事先将UIBezierPath绘制到上下文中

时间:2012-11-02 20:57:23

标签: iphone ios xcode uibezierpath cgbitmapcontextcreate

这是我的第一个问题所以请耐心等待我!

我试图写一个简单的绘图应用程序基本上,我以前使用的是Core Graphics,唯一的问题是它太慢了,当我用我的手指画它时,它已经落后了,真是太多了!

所以,现在我正在尝试使用UIBezier路径进行绘制,正如我所理解的那样,它更快,它就是这样!

当我使用Core Graphics时,为了保持绘图速度的提高,我正在绘制一个我创建的自定义位图上下文,它随着我的绘制而不断更新。

所以,我绘制了自定义Bitmap上下文,然后将CGImageRef设置为使用 -

在该上下文中绘制的内容
cacheImage = CGBitmapContextCreateImage(imageContext); 

然后使用 -

将其拉回到位图上下文中
CGContextDrawImage(imageContext, self.bounds, cacheImage); )

我也是这样做的,所以当我更改正在绘制的线条的颜色时,如果有意义的话,绘图的其余部分保持原来的状态。

现在我遇到的问题是这个。

我试图使用 -

将UIBezier路径绘制到我的图像上下文
imageContext = UIGraphicsGetCurrentContext();

UIGraphicsPushContext(imageContext);


[path stroke];


if(imageContext != nil){
    cacheImage = CGBitmapContextCreateImage(imageContext);  //invalid context here so added to solve

}


CGContextScaleCTM(imageContext, 1, -1); // using this as UIBezier 

CGContextDrawImage(imageContext, self.bounds, cacheImage); // draws the current context;

[path removeAllPoints];

CGImageRelease(cacheImage); // releases the image to solve memory errors.

路径是我的UIBezierPath。设置的所有路径都是在触摸开始和触摸后移动然后调用[self setNeedsDisplay];调用drawRect。

当我绘制时,它要么没有正确地将CGImageRef绘制到上下文中,要么是,但是当它捕获缓存图像时,它从某个地方捕获白色背景,而不仅仅是路径,所以它的粘贴在整个图像上,最后一条路径与白色背景填充一起绘制,因此即使视图背景颜色为clearColor,您也无法看到用于构建图像的最后一条路径。

我真的希望我有意义,我只是花了太多时间在这上面,它完全耗尽了我。继承人我正在使用的绘图方法 -

这是为了创建图像上下文 -

 -(CGContextRef) myCreateBitmapContext: (int) pixelsWide:(int) pixelsHigh{


    imageContext = NULL;

    CGColorSpaceRef colorSpace; // creating a colorspaceref
    void *          bitmapData; // creating bitmap data
    int             bitmapByteCount; // the bytes per count
    int             bitmapBytesPerRow; // number of bytes per row

    bitmapBytesPerRow   = (pixelsWide * 4); // calculating how many bytes per row the context     needs
    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh); // how many bytes there are in     total

    colorSpace = CGColorSpaceCreateDeviceRGB(); // setting the colourspaceRef

    bitmapData = malloc( bitmapByteCount ); // calculating the data

    if (bitmapData == NULL)
    {
        //NSLog(@"Memory not allocated!");
        return NULL;
    }


    imageContext = CGBitmapContextCreate (bitmapData,
                                          pixelsWide,
                                          pixelsHigh,
                                          8, // bits per component
                                          bitmapBytesPerRow,
                                          colorSpace,kCGImageAlphaPremultipliedLast);

    if (imageContext== NULL)
    {
        free (bitmapData);
        NSLog(@"context not created allocated!");
        return NULL;
    }
    CGColorSpaceRelease( colorSpace ); //releasing the colorspace

    CGContextSetRGBFillColor(imageContext, 1.0, 1.0, 1.0, 0.0); // filling the bitmap with a     white background
    CGContextFillRect(imageContext, self.bounds);
    CGContextSetShouldAntialias(imageContext, YES); 




    return imageContext; 
    }

继续我的画 -

 -(void)drawRect:(CGRect)rect
{

    DataClass *data = [DataClass sharedInstance];



    [data.lineColor setStroke];
    [path setLineWidth:data.lineWidth];
    imageContext = UIGraphicsGetCurrentContext();

    UIGraphicsPushContext(imageContext);


    [path stroke];


    if(imageContext != nil){
        cacheImage = CGBitmapContextCreateImage(imageContext);  
    }


    CGContextScaleCTM(imageContext, 1, -1); // this one


    CGContextDrawImage(imageContext, self.bounds, cacheImage); // draws the current     context;



    [path removeAllPoints];

    CGImageRelease(cacheImage); // releases the image to solve memory errors.



}





 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{


    DataClass *data = [DataClass sharedInstance];

    CGContextSetStrokeColorWithColor(imageContext, [data.lineColor CGColor]);

    ctr = 0;
    UITouch *touch2 = [touches anyObject];
    pts[0] = [touch2 locationInView:self];

}


 -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];


    ctr++;
    pts[ctr] = p;
    if (ctr == 4)
    {

        pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move     the endpoint to the middle of the line joining the second control point of the first Bezier     segment and the first control point of the second Bezier segment
        [path moveToPoint:pts[0]];
        [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a     cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]
        //[data.lineColor setStroke];

        [self setNeedsDisplay];
        // replace points and get ready to handle the next segment
        pts[0] = pts[3];
        pts[1] = pts[4];
        ctr = 1;
    }
}


 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    [path removeAllPoints];
    [self setNeedsDisplay];
    ctr = 0;
}

'path'是我的UIBezierPath 'cacheImage'是一个CGImageRef 'imageContext'是一个CGContextRef

非常感谢任何帮助!如果您能想到更好的方法,请告诉我!然而,我确实需要缓存图像具有透明背景,因此它只是可见的路径,因为我将在以后使用它时应用这些工作!

编辑此外,我每次都会删除积分以保持绘图速度,只是让你知道!

提前致谢:)

1 个答案:

答案 0 :(得分:1)

嗯,这是一个很大的问题。一个潜在的领导是验证你是否准确绘制了你需要的东西(而不是整个图像),并将不变位图与那些在多个层中主动变异的区域/区域分开。