无法调用:drawAtPoint用于从方法返回的UIImage *

时间:2014-03-17 07:46:42

标签: ios objective-c uiimage

我目前正在关注Matt Neuburg编程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)];

似乎我无法将drawAtPoint:发送到[self arrowImage]。我已经检查了,返回值不是零。

有人可以帮我这个吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

CGContextTranslateCTM(con, 80, 0);方法移除arrowImage,它会起作用。