テキストを画像に変換し、その画像をサブビューとして使用しようとしています。しかし、私はいくつかのエラーが発生します。私は別の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
感謝を高度に!リンク以下よりご覧ください
これはメソッドの奇妙な名前です。その名前から、私はイメージのパラメータを最初に、テキストのパラメータを期待したいと思います。あなたはそれらを誤って混ぜていないと確信していますか?どのように実際にメソッドを呼び出すのですか?あなたの質問の呼び出し元の行はほとんどコンパイルされません。 – Codo
あなたはどうしたらこのメソッドを呼び出すべきだと思いますか?私はちょうどtextInsertが私がコンパイルしたいtextを持つnsstringで、textImageが私がコンパイルされたテキストイメージを表示するために使用しているUIImageである上記のものを使用します。 –
メソッド名では、各コロンの直前の単語は、通常、コロンの直後のパラメータのタイプを示します。だから良い名前は '(UIImage *)burnText:(NSString *)text intoImage:(UIImage *)image'です。 (BTW:4回目のコーリングラインを見て、私が間違っていて、それがコンパイルされていることを理解しました。メソッド名がちょっと混乱しています。) – Codo