作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在关注 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/
我是一名优秀的程序员,十分优秀!