8

数字が入った円を描いてみます。

   -(void)drawRect:(CGRect)rect {
    //Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 4.0);
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextBeginPath(context);
    CGContextAddEllipseInRect(context, rect);
    CGContextDrawPath(context, kCGPathFillStroke);

    // Label and index code
    UILabel* circleLabel = [[UILabel alloc] init];
    NSString *i=[Index stringValue];
    [circleLabel setText:i];
    [self addSubview:circleLabel];




}

自分のメソッドからdrawRectメソッドを呼び出します。

-(void)addPhotoIndex:(NSNumber*)tempNumber withCoordinate:(NSString*)currentTouchPos{
    self.coord= CGPointFromString(currentTouchPos);
    CGRect rect= CGRectMake ((coord.x - 5), (coord.y- 5), 5, 5);
    NSNumber *Index= tempNumber;
    [self drawRect:rect];




}

それ自体がViewControllerから呼び出されること。

しかし、これは何も描画しておらず、エラーも発生していません。誰か助けてくれませんか?

解決:

最後に、これはそれが機能したものです:

  -(void)addPhotoIndex:(NSNumber*)tempNumber withCoordinate:(NSString*)currentTouchPos{

    self.coord= CGPointFromString(currentTouchPos);
    CGRect rect= CGRectMake ((coord.x - 3), (coord.y- 3), 3, 3);
    NSNumber *Index= tempNumber;
    NSString *i=[Index stringValue];
    UIFont *font = [UIFont systemFontOfSize:15.0];

    // Drawing Point
    UIGraphicsBeginImageContext(self.frame.size);
    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 3.0);
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextBeginPath(context);
    CGContextAddEllipseInRect(context, rect);
    CGContextDrawPath(context, kCGPathFillStroke);
    [ i drawAtPoint:self.coord withFont:font];
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();


}

インデックス番号の付いたポイントが正しい位置に並べて描画されます:)お役に立ててありがとうございます。

4

2 に答える 2

4

使用する必要のある方法はではありdrawRect:ませんdrawInRect:

また、これはで設定されているため、独自のコンテキストを作成する必要はありません。drawRect:

- (void)drawRect:(CGRect)rect;
{
  // UIGraphicsBeginImageContext(self.bounds.size); <-- not needed
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetLineWidth(context, 4.0);
  CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
  CGContextBeginPath(context);
  CGContextAddEllipseInRect(context, rect);
  CGContextDrawPath(context, kCGPathFillStroke); // Or kCGPathFill

  /*
   * As @Jonathan Cichon points out you should not add subview's here
   * either add them elsewhere or draw the string manually
   */
  // UILabel* circleLabel = [[UILabel alloc] init];
  // NSString *i=[Index stringValue];
  // [circleLabel setText:i];
  // [self addSubview:circleLabel];
  // UIGraphicsEndImageContext(); <-- not needed
}
于 2012-11-05T13:16:10.730 に答える
1

- (void)drawInRect:(CGRect)rectのサブクラスで実装されていると仮定しますUIView。その場合は、を使用します- (void)drawRect:(CGRect)rect。そのため、メソッドが呼び出されたときに、描画のコンテキストがすでに割り当てられています。UIGraphicsBeginImageContextと呼び出しを削除しUIGraphicsEndImageContextます。また、にサブビューを追加しないでください。代わりdrawRectに使用してください[i drawInRect:withFont:]

于 2012-11-05T13:17:08.150 に答える