ios - 如何使用 Quartz 2D 和 Grand CEntral Dispatch 以编程方式生成 UIImage
<p><p>现在我有一个使用 Quartz 2D 以编程方式在水龙头上生成的图像。我想将它与大型中央调度结合使用,以便可以在另一个 cpu 上创建它,并在完成时触发通常的动画淡入淡出。现在我在这篇文章的底部使用下面的代码,但我得到了这些无效的上下文错误。有没有办法做到这一点,还是我不走运?</p>
<blockquote>
<p>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</p>
</blockquote>
<p>我的代码:</p>
<pre><code> dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
self.view.contentScaleFactor=[ scale];
CGSize sizeofText=]];
CGSize size;
size.width=1024;
size.height=1024;
UIImage *leftCap = pathForResource:@"leftcap@2x" ofType:@"png"]];
UIImage *center = pathForResource:@"center@2x" ofType:@"png"]];
UIImage *rightCap = pathForResource:@"rightcap@2x" ofType:@"png"]];
UIImage *spike = 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*)+(sizeofText.width)+(43*);
height=60*;
//draw calls
, 19.0f*, 54.0f*)];
,1024- 54.0f*,(sizeofText.width), 54.0f*)];
,1024- 54.0f*, 43.0f*, 54.0f*)];
)/2.0f),1024-(43.5*)- 27.0f*, 32.0f*, 27.0f*)];
CGContextSelectFont(context, "Helvetica-Bold", 17.5*, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetTextPosition(context,19.f*,1024- (7.5*)-(sizeofText.height));
CGContextShowText(context, , strlen());
image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
});
dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
NSLog(@"done. I would trigger the fade in animation ehre");
});
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>除了使用 CGBitmapContextCreate 代替 UIGraphicsBeginImageContextWithOptions 之外,我做了类似的事情。看起来您的上下文没有被创建 - 可能是因为您在后台线程上调用 UI* 函数。</p>
<pre><code>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);
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何使用 Quartz 2D 和 Grand CEntral Dispatch 以编程方式生成 UIImage,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/9147692/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/9147692/
</a>
</p>
页:
[1]