在UIScrollView中绘图
我目前正在研究一个项目,使您能够在scrollview中进行触摸绘制。 唯一的问题是,它不让我画到滚动视图的底部。 我想这与UIGraphicsgetCurrentContext()
。 任何帮助将是真棒! 至于我到目前为止
- (void)drawRect:(CGRect)rect { //Here UIGraphicsBeginImageContextWithOptions(CGSizeMake(1630, 2400), YES, 5); __block CGContextRef context = UIGraphicsGetCurrentContext(); //CGContextAddRect(context, CGRectMake(0, 0, 1024, 1620)); //[contentView.layer renderInContext:context]; CGContextSaveGState(context); CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]); CGContextSetLineWidth(context, 4.0f); CGContextSetLineCap(context, kCGLineCapRound); CGContextSetLineJoin(context, kCGLineJoinRound); [[ProblemStore sharedProblemStore] mapCurrentSolutionStrokes:^(SolutionStroke *stroke, NSUInteger strokeNum) { [self drawStroke:stroke inContext:context]; }]; CGContextRestoreGState(context); UIGraphicsEndImageContext(); }
如果这个drawRect
方法是针对某个UIView
子类的,那么可以简化它:
- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]); CGContextSetLineWidth(context, 4.0f); CGContextSetLineCap(context, kCGLineCapRound); CGContextSetLineJoin(context, kCGLineJoinRound); [[ProblemStore sharedProblemStore] mapCurrentSolutionStrokes:^(SolutionStroke *stroke, NSUInteger strokeNum) { [self drawStroke:stroke inContext:context]; }]; }
当你想渲染一些视图并用UIGraphicsGetImageFromCurrentImageContext
检索图像时,通常使用UIGraphicsBeginImageContextWithOptions
,但是在自定义视图的drawRect
本身中通常不使用UIGraphicsBeginImageContextWithOptions
。 我会分开drawRect
function从图像保存逻辑(假设你甚至需要/后者)。
所以,我个人会创build一个适当大小的自定义视图(例如1630 x 2400),将其添加到滚动视图,并使用上述作为该自定义视图的drawRect
。
根据为什么您的演绎不是下拉到滚动视图的底部,我怀疑这是与您的UIGraphicsBeginImageContextWithOptions
select5
的scale
。 通常使用1
(非视网膜), 2
(视网膜)或0
(使用主屏幕的比例)。
另外,您不需要CGContextRef
的__block
限定符。 你可以不用它就可以访问context
variables。