有効なコンテキストを返すには、適切な領域にいる必要があります。
つまり、基本的に、このコードが含まれている必要があるdrawRect:
か、使用して画像コンテキストを作成する必要があることを意味しますUIGraphicsBeginImageContext
更新: DrawRect:
これdrawRect:
は、Core Graphics を使用してカスタム描画を行うためのアクセス ポイントを提供する、各 UIView に対して呼び出される特別なメソッドです。これの最も一般的な使用法は、カスタム UIView オブジェクトを作成することArrowView
です。次に、コードを使用してオーバーライドdrawRect:
します。
- (void)drawRect:(CGRect)rect
{
CGPoint p1 = {10, 10};
CGSize size;
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawArrowWithContext:context atPoint:p1 withSize:size lineWidth:400 arrowHeight:400];
}
更新: 画像のコンテキスト
カスタム Core Graphics 描画を利用する 2 番目の方法は、imageContext を作成し、その結果を収集することです。
したがって、まずイメージ コンテキストを作成し、描画コードを実行してから、それを既存のビューに追加できる UIImage に変換します。
UIGraphicsBeginImageContext(CGSizeMake(400.0, 400.0));
CGPoint p1 = {10, 10};
CGSize size;
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawArrowWithContext:context atPoint:p1 withSize:size lineWidth:400 arrowHeight:400];
// converts your context into a UIImage
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// Adds that image into an imageView and sticks it on the screen.
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];