0

テキストを画像に変換し、その画像をサブビューとして使用しようとしています。ただし、いくつかのエラーが発生します。別のスタックオーバーフローの投稿からスクリプトの一部を取得しました。

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

ありがとうございます!

4

2 に答える 2

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

アクティブなグラフィック コンテキストがありません。IOW、あなたは抽選をリクエストしていますが、オペレーターも宛先もありません。

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

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

于 2012-04-16T18:35:13.110 に答える
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();

}

詳細については、次のリンクを参照してください...

Quartz 2D がテキストを描画する方法

うまくいけば、これはあなたを助けるでしょう....寒さ...

于 2012-04-16T17:26:25.787 に答える