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

标签 ios opengl-es uiimage grand-central-dispatch quartz-2d

现在我已经使用 Quartz 2D 以编程方式在水龙头上生成了图像。我想将它与中央调度结合使用,这样它就可以在另一个 CPU 上创建,并在完成时触发通常的动画淡入淡出。现在我使用本文底部的以下代码,但我收到这些无效的上下文错误。有办法做到这一点还是我运气不好?

CGContextTranslateCTM: invalid context 0x0 CGContextScaleCTM: invalid context 0x0 CGContextSaveGState: invalid context 0x0 CGContextSetCompositeOperation: invalid context 0x0 CGContextFillRects: invalid context 0x0 CGContextRestoreGState: invalid context 0x0 CGContextDrawImage: invalid context 0x0

我的代码:

    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"); 
    });

最佳答案

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

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);

关于ios - 如何使用 Quartz 2D 和 Grand CEntral Dispatch 以编程方式生成 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9147692/

相关文章:

ios - POST API 调用在处理 JSON 正文时遇到问题

ios - 此解码器只会解码采用 NSSecureCoding 的类

ios - objective-c - Parse.com 保存和读取地理位置 IOS

ios - 需要在 UIActivitycontroller 中显示腾讯微博图标

ios - GPUImage 创建一个自定义过滤器来更改选定的颜色

ios - 裁剪要与GLKTextureLoader一起使用的UIImage

ios - Opengl ES 3.1 支持 ios 吗?

ios - Sprite Kit & 播放声音导致应用程序终止

ios - 带有 AFNetworking 的 UIImage

objective-c - UIScrollView + UIImage 和缩放 : not fluid