[英]Simple graph draw task for iPhone
我需要绘制一个简单的图形,但是我还没有使用自定义绘制iPhone图形的经验,所以希望有人可以帮助我。
任务很简单:我需要从资源中的.png文件中绘制图形背景,并从背景中某些位置上的捆绑的.png文件中绘制点。
为了进行绘制,我从UIView创建了后代,并使用以下代码行:
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(0, 0, 250, 500);
CGContextDrawImage(context, rect, [[UIImage imageNamed:@"graph.png"] CGImage]);
但是它不起作用。
第一个问题,我无法解决-UIGraphicsGetCurrentContext返回nil。
你能帮助我吗?
我用这段代码解决了我的问题。
- (void) drawGrid{
UIImage *sourceImage = [UIImage imageNamed:@"sungraph.png"];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(sourceImage.size.width, sourceImage.size.height), NO, 0);
CGRect rectangle = CGRectMake(0, 0, sourceImage.size.width, sourceImage.size.height);
self.frame = rectangle;
[sourceImage drawInRect:rectangle];
}
- (void) finalizeImage{
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imgView = [[UIImageView alloc] initWithImage:newImage];
imgView.frame = CGRectMake(0, 0, newImage.size.width, newImage.size.height);
[self addSubview:imgView];
}
从类的initWithFrame中 ,我调用第一个函数。 之后,我将一些数据添加到图形中(绘制线条等),并在最后调用finalizeImage 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.