2012-02-05 55 views
2

现在,我没有使用Quartz 2D以编程方式在Tap上生成图像。我想将它与grand central dispatch结合使用,以便它可以在另一个cpu上创建,并在动画完成时触发通常的动画淡入淡出。现在,我在这篇文章的底部使用了下面的代码,但即时获取这些无效的上下文错误。有没有办法做到这一点,或者我运气不好?如何用Quartz 2D和Grand CEntral Dispatch以编程方式生成UIImage

CGContextTranslateCTM:无效的情况下为0x0 CGContextScaleCTM:无效的情况下为0x0 CGContextSaveGState:无效的情况下为0x0 CGContextSetCompositeOperation:无效的情况下为0x0 CGContextFillRects:无效的情况下为0x0 CGContextRestoreGState:无效的情况下为0x0 CGContextDrawImage:无效的情况下为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:@"[email protected]" ofType:@"png"]]; 
     UIImage *center = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"[email protected]" ofType:@"png"]]; 
     UIImage *rightCap = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"[email protected]" ofType:@"png"]]; 
     UIImage *spike = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"[email protected]" 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"); 
    }); 

回答

4

我做了类似的事情,除了我使用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); 
+0

如果奥尤不介意,究竟你创建方面,我搬到了被主线程创建的UIGraphicsBeginImageContextwithOptions像这样,但它仍然不工作:http://pastebin.com/JZi7esx7 – jfisk 2012-02-05 17:23:53

+0

我已经现在添加一个代码示例到我的答案中 – 2012-02-06 15:06:38

相关问题