加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 17:22:08 所属栏目:百科 来源:网络整理
导读:这是我的第一个问题所以请耐心等待! 我试图写一个简单的绘图应用程序基本上,我之前使用的是Core Graphics,唯一的问题是它太慢了,当我用我的手指画它时滞后,很多很多! 所以,现在我正在尝试使用UIBezier路径进行绘制,正如我所理解的那样,它更快,它就是这样!
这是我的第一个问题所以请耐心等待!

我试图写一个简单的绘图应用程序基本上,我之前使用的是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,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,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); // this one


    CGContextDrawImage(imageContext,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

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

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

提前致谢 :)

解决方法

嗯,这是一个很大的问题.一个潜在的领先优势是验证您是否准确绘制了所需的内容(而不是始终绘制整个图像),并将不变位图与那些在多个层中主动变异的区域/区域进行划分.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读