gpt4 book ai didi

ios - 无法调用 :drawAtPoint for a UIImage* returned from a method

转载 作者:行者123 更新时间:2023-11-29 03:08:50 25 4
gpt4 key购买 nike

我目前正在关注 Matt Neuburg 编写的 Programming iOS 7 中的代码。

我在我的自定义 View (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]。我查过了,返回值不是nil。

有人可以帮我解决这个问题吗?

谢谢。

最佳答案

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

关于ios - 无法调用 :drawAtPoint for a UIImage* returned from a method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22449092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com