我错过了什么?卡住了几个小时,drawRect按预期绘制,从一个viewcontoller调用getTheImage()UIGraphicsGetImageFromCurrentImageContext()返回nil;尝试了很多变化。 任何帮助表示赞赏。
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsBeginImageContext(self.bounds.size);
CGContextSetLineWidth(context, 5.0);
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor]CGColor]);
CGContextMoveToPoint(context,100.0,100.0);
CGContextAddLineToPoint(context,200.0,200.0);
CGContextMoveToPoint(context,200.0,200.0);
CGContextAddLineToPoint(context,200.0,400.0);
CGContextStrokePath(context);
}
-(UIImage *)getTheImage
{
UIImage *drawImage = UIGraphicsGetImageFromCurrentImageContext();
NSLog(@"drawImage %@",drawImage);
return drawImage;
}
答案 0 :(得分:0)
你能试试吗?
-(UIImage *)getTheImage
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *drawImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"drawImage %@",drawImage);
return drawImage;
}
答案 1 :(得分:0)
只有当基于位图的图形上下文是当前图形上下文时,才应调用此函数。如果当前上下文为nil或者不是通过调用UIGraphicsBeginImageContext创建的,则此函数返回nil。
看来你的电话不在上下文中。