2012-04-16 26 views
0

テキストを画像に変換し、その画像をサブビューとして使用しようとしています。しかし、私はいくつかのエラーが発生します。私は別のstackoverflowの投稿からスクリプトの部分を得た;テキストを画像に変換しようとしているときにCGContextエラーが発生しました

/* 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 

感謝を高度に!リンク以下よりご覧ください

+0

これはメソッドの奇妙な名前です。その名前から、私はイメージのパラメータを最初に、テキストのパラメータを期待したいと思います。あなたはそれらを誤って混ぜていないと確信していますか?どのように実際にメソッドを呼び出すのですか?あなたの質問の呼び出し元の行はほとんどコンパイルされません。 – Codo

+0

あなたはどうしたらこのメソッドを呼び出すべきだと思いますか?私はちょうどtextInsertが私がコンパイルしたいtextを持つnsstringで、textImageが私がコンパイルされたテキストイメージを表示するために使用しているUIImageである上記のものを使用します。 –

+0

メソッド名では、各コロンの直前の単語は、通常、コロンの直後のパラメータのタイプを示します。だから良い名前は '(UIImage *)burnText:(NSString *)text intoImage:(UIImage *)image'です。 (BTW:4回目のコーリングラインを見て、私が間違っていて、それがコンパイルされていることを理解しました。メソッド名がちょっと混乱しています。) – Codo

答えて

0
////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(); 

} 

...

How Quartz 2D Draws Text

希望、これはあなたを助けるでしょう....チル...

0
<Error>: CGContextSetFillColorWithColor: invalid context 0x0 
... 

アクティブなグラフィックスはありませんコンテキスト。 IOW、あなたはドローを要求していますが、オペレータや宛先はありません。

グラフィックスコンテキストがローカルに存在する場合(たとえば、drawRect:が呼び出された場合など)、またはプロセスのコンテキストを明示的に作成する必要があります(例:CGBitmapContextCreate)。

UIGraphicsBeginImageContextを使用している場合は、メインスレッドからのものである必要があります。任意のスレッドからCGBitmapContextを使用できます。

関連する問題