Iphone 尝试将文本转换为图像时出现CGContext错误

Iphone 尝试将文本转换为图像时出现CGContext错误,iphone,objective-c,ios,xcode,cgcontext,Iphone,Objective C,Ios,Xcode,Cgcontext,我正在尝试将文本转换为图像,并将该图像用作子视图。然而,我得到了一些错误。我从另一个stackoverflow帖子上得到了这段脚本 /* Creates an image with a home-grown graphics context, burns the supplied string into it. */ - (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img { UIGraphicsBeginImag

我正在尝试将文本转换为图像,并将该图像用作子视图。然而,我得到了一些错误。我从另一个stackoverflow帖子上得到了这段脚本

/* Creates an image with a home-grown graphics context, burns the supplied string into it. */
- (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img {

    UIGraphicsBeginImageContext(img.size);

    CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height);
    [img drawInRect:aRectangle];

    [[UIColor redColor] set];           // set text color
    NSInteger fontSize = 14;
    if ( [text length] > 200 ) {
        fontSize = 10;
    }
    UIFont *font = [UIFont boldSystemFontOfSize: fontSize];     // set text font

    [ text drawInRect : aRectangle                      // render the text
             withFont : font
        lineBreakMode : UILineBreakModeTailTruncation  // clip overflow from end of last line
            alignment : UITextAlignmentCenter ];

    UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();   // extract the image
    UIGraphicsEndImageContext();     // clean  up the context.
    return theImage;
}
我称这段代码为:

[self burnTextIntoImage:textInsert :textImage];
我得到的错误是:

<Error>: CGContextSetFillColorWithColor: invalid context 0x0
<Error>: CGContextSetStrokeColorWithColor: invalid context 0x0
<Error>: CGContextSetFont: invalid context 0x0
<Error>: CGContextSetTextMatrix: invalid context 0x0
<Error>: CGContextSetFontSize: invalid context 0x0
<Error>: CGContextSetTextPosition: invalid context 0x0
<Error>: CGContextShowGlyphsWithAdvances: invalid context 0x0
提前感谢

////Drawing text using Quartz 2D in an iOS application

- (void)drawRect:(CGRect)rect

{

  CGContextRef theContext = UIGraphicsGetCurrentContext();

  CGRect viewBounds = self.bounds;



  CGContextTranslateCTM(theContext, 0, viewBounds.size.height);

  CGContextScaleCTM(theContext, 1, -1);



  // Draw the text using the MyDrawText function

  MyDrawText(theContext, viewBounds);
}

 ////Drawing text
void MyDrawText (CGContextRef myContext, CGRect contextRect) // 1

{

float w, h;

w = contextRect.size.width;

h = contextRect.size.height;



CGAffineTransform myTextTransform; // 2

CGContextSelectFont (myContext, // 3

                "Helvetica-Bold",

                 h/10,

                 kCGEncodingMacRoman);

CGContextSetCharacterSpacing (myContext, 10); // 4

CGContextSetTextDrawingMode (myContext, kCGTextFillStroke); // 5



CGContextSetRGBFillColor (myContext, 0, 1, 0, .5); // 6

CGContextSetRGBStrokeColor (myContext, 0, 0, 1, 1); // 7

myTextTransform =  CGAffineTransformMakeRotation  (MyRadians (45)); // 8

CGContextSetTextMatrix (myContext, myTextTransform); // 9

CGContextShowTextAtPoint (myContext, 40, 0, "Quartz 2D", 9); // 10

UIImage *theImage=  UIGraphicsGetImageFromCurrentImageContext();

 UIGraphicsEndImageContext();

}
欲了解更多信息,请访问以下链接

希望,这会帮助你…冷静

<Error>: CGContextSetFillColorWithColor: invalid context 0x0
...
没有活动的图形上下文。您看,您正在请求抽签,但没有操作员或目的地

当图形上下文在本地存在时(例如调用drawRect:时),或者必须为进程显式创建上下文(例如CGBitmapContextCreate),需要执行此调用


如果您使用的是UIGraphicsBeginImageContext,那么它应该仅来自主线程。您可以从任何线程使用CGBitmapContext。

这是一个奇怪的方法名称。根据它的名称,我希望图像参数是第一个,文本参数是第二个。你确定你没有不小心把它们弄混了吗?您如何真正调用该方法?你问题中的调用行很难编译。那么你认为我应该如何调用该方法?我只使用上面写的方法,其中textInsert是一个nsstring,包含我要编译的文本,textImage是一个UIImage,我用它来显示编译后的文本图像。在方法名称中,每个冒号前面的一个或多个单词通常指示冒号后面的参数类型。所以一个好名字应该是UIImage*burnText:NSString*text-intoImage:UIImage*image。顺便说一句:在第四次查看电话线之后,我发现我可能是错的,而且它是编译的。你的方法名对我来说太混乱了。我已经更改了它,但仍然不起作用。我得到了和以前一样的错误。在发布的代码中没有明显的错误。因此,问题可能是img参数为nil,您是否检查了该参数,或者您没有在主线程上运行代码。