无法为方法返回的UIImage*调用:drawpoint



我目前正在遵循Matt Neuburg在Programming iOS 7中的代码。

我在我的自定义视图(ArrowsView)中输入了以下代码。

#import "ArrowsView.h"
@implementation ArrowsView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.opaque = NO;
    }
    return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    [[self arrowImage] drawAtPoint:CGPointMake(0, 0)];

//    UIGraphicsBeginImageContextWithOptions(CGSizeMake(40, 100), NO, 0.0);
//    // obtain the current graphics context.
////    CGContextRef con = UIGraphicsGetCurrentContext();
//    CGContextTranslateCTM(con, 80, 0);
//    // draw the arrow into the current context.
//    // draw the black (by default) vertical line, the shaft of the arrow.
//    CGContextMoveToPoint(con, 20, 100);
//    CGContextAddLineToPoint(con, 20, 19);
//    CGContextSetLineWidth(con, 20);
//    CGContextStrokePath(con);
//    // draw a red triangle, the point of the arrow.
//    CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]);
//    CGContextMoveToPoint(con, 0, 25);
//    CGContextAddLineToPoint(con, 20, 0);
//    CGContextAddLineToPoint(con, 40, 25);
//    CGContextFillPath(con);
//    // snip a triangle out of the shaft by drawing in clear blend mode.
//    CGContextMoveToPoint(con, 10, 101);
//    CGContextAddLineToPoint(con, 20, 90);
//    CGContextAddLineToPoint(con, 30, 101);
//    CGContextSetBlendMode(con, kCGBlendModeClear);
//    CGContextFillPath(con);
//    UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    [im drawAtPoint:CGPointMake(0, 0)];
}
- (UIImage*) arrowImage {
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(40, 100), NO, 0.0);
    // obtain the current graphics context.
    CGContextRef con = UIGraphicsGetCurrentContext();
    // draw it at (0, 0);
    CGContextTranslateCTM(con, 80, 0);
    // draw the arrow into the current context.
    // draw the black (by default) vertical line, the shaft of the arrow.
    CGContextMoveToPoint(con, 20, 100);
    CGContextAddLineToPoint(con, 20, 19);
    CGContextSetLineWidth(con, 20);
    CGContextStrokePath(con);
    // draw a red triangle, the point of the arrow.
    CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]);
    CGContextMoveToPoint(con, 0, 25);
    CGContextAddLineToPoint(con, 20, 0);
    CGContextAddLineToPoint(con, 40, 25);
    CGContextFillPath(con);
    // snip a triangle out of the shaft by drawing in clear blend mode.
    CGContextMoveToPoint(con, 10, 101);
    CGContextAddLineToPoint(con, 20, 90);
    CGContextAddLineToPoint(con, 30, 101);
    CGContextSetBlendMode(con, kCGBlendModeClear);
    CGContextFillPath(con);
    UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return im;
}

@end

注意在drawRect:中注释掉的大块绘图调用。我能够得到那块代码来绘制我需要在屏幕上看到的东西。

然而,我无法让前一行代码在drawRect中工作:。具体来说,这行代码不起作用:

[[self arrowImage] drawAtPoint:CGPointMake(0, 0)];

似乎我无法发送drawpoint:到[self arrowImage]。我检查过了,返回值不是nil

有人能帮我一下吗?

谢谢。

arrowImage方法中删除CGContextTranslateCTM(con, 80, 0);,它将工作

最新更新