• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 19:18:53 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

现在我有一个使用 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"); 
    });



Best Answer-推荐答案


除了使用 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap