首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Quartz 2D和Grand CEntral Dispatch以编程方式生成CEntral

如何使用Quartz 2D和Grand CEntral Dispatch以编程方式生成CEntral
EN

Stack Overflow用户
提问于 2012-02-05 07:31:45
回答 1查看 3.7K关注 0票数 2

现在,我有一个图像生成在一个点击与石英2D编程。我想将它与中央调度结合使用,这样就可以在另一个cpu上创建它,并在它完成时触发动画中的通常淡出。现在,我在文章底部使用了下面的代码,但是我得到了这些无效的上下文错误。有办法这样做吗?还是我运气不好?

CGContextTranslateCTM:无效上下文0x0 CGContextScaleCTM:无效上下文0x0 CGContextSaveGState:无效上下文0x0 CGContextSetCompositeOperation:无效上下文0x0 CGContextFillRects:无效上下文0x0 CGContextRestoreGState:无效上下文0x0 CGContextDrawImage:无效上下文0x0

我的代码:

代码语言:javascript
复制
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
        self.view.contentScaleFactor=[[UIScreen mainScreen] scale];
        CGSize sizeofText=[stopName sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:17.5*[self.view contentScaleFactor]]];
        CGSize size;
        size.width=1024;
        size.height=1024;

        UIImage *leftCap = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"leftcap@2x" ofType:@"png"]];
        UIImage *center = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"center@2x" ofType:@"png"]];
        UIImage *rightCap = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"rightcap@2x" ofType:@"png"]];
        UIImage *spike = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"spike@2x" ofType:@"png"]];
        UIGraphicsBeginImageContextWithOptions(size,false,0);

        CGContextRef context = UIGraphicsGetCurrentContext(); //get the context we just made above
        CGContextTranslateCTM(context, 0,size.height);
        CGContextScaleCTM(context, 1, -1);

        width=(19*[self.view contentScaleFactor])+(sizeofText.width)+(43*[self.view contentScaleFactor]);

        height=60*[self.view contentScaleFactor];

        //draw calls
        [leftCap drawInRect:CGRectMake(0,1024- 54.0f*[self.view contentScaleFactor], 19.0f*[self.view contentScaleFactor], 54.0f*[self.view contentScaleFactor])];    
        [center drawInRect:CGRectMake(19.0f*[self.view contentScaleFactor],1024- 54.0f*[self.view contentScaleFactor],(sizeofText.width), 54.0f*[self.view contentScaleFactor])];
        [rightCap drawInRect:CGRectMake((sizeofText.width)+19*[self.view contentScaleFactor],1024- 54.0f*[self.view contentScaleFactor], 43.0f*[self.view contentScaleFactor], 54.0f*[self.view contentScaleFactor])];
        [spike drawInRect:CGRectMake((width/2)-((32.0f*[self.view contentScaleFactor])/2.0f),1024-(43.5*[self.view contentScaleFactor])- 27.0f*[self.view contentScaleFactor], 32.0f*[self.view contentScaleFactor], 27.0f*[self.view contentScaleFactor])];

        CGContextSelectFont(context, "Helvetica-Bold", 17.5*[self.view contentScaleFactor], kCGEncodingMacRoman);
        CGContextSetTextDrawingMode(context, kCGTextFill);
        CGContextSetTextPosition(context,19.f*[self.view contentScaleFactor],1024- (7.5*[self.view contentScaleFactor])-(sizeofText.height));
        CGContextShowText(context, [stopName UTF8String], strlen([stopName UTF8String]));

        image=UIGraphicsGetImageFromCurrentImageContext();



        UIGraphicsEndImageContext();

    });



    dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
        NSLog(@"done. I would trigger the fade in animation ehre"); 
    });
EN

回答 1

Stack Overflow用户

发布于 2012-02-05 07:36:38

我也做了类似的事情,除了用CGBitmapContextCreate代替UIGraphicsBeginImageContextWithOptions。看起来您的上下文没有被创建--可能是因为您在后台线程上调用了UI*函数。

代码语言:javascript
复制
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef _composedImageContext = CGBitmapContextCreate(NULL, 
                                              width, 
                                              height, 
                                              8, 
                                              width*4, 
                                              rgbColorSpace, 
                                              kCGImageAlphaPremultipliedFirst);

CGColorSpaceRelease( rgbColorSpace );


// draw your things into _composedImageContext

//finally turn the context into a CGImage
CGImageRef cgImage = CGBitmapContextCreateImage(_composedImageContext);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9147692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档