I am currently working on a project that enables you to draw with touches in a scrollview. The only problem is, it doesn't let me draw to the bottom part of the scrollview. I am thinking it has something to do with UIGraphicsgetCurrentContext()
.
Any help will be awesome!
Heres what I have so far
- (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();
}
If this
drawRect
method is for someUIView
subclass, you can simplify it:You generally use the
UIGraphicsBeginImageContextWithOptions
when you want to render some view and retrieve an image withUIGraphicsGetImageFromCurrentImageContext
, but you don't generally useUIGraphicsBeginImageContextWithOptions
in your custom view'sdrawRect
, itself. I would separate thedrawRect
functionality from the image saving logic (assuming you even need/want the latter).So, I personally would create a custom view of the appropriate size (e.g. 1,630 x 2,400), add it to the scroll view, and use the above as the
drawRect
for that custom view.In terms of why your rendition is not drawing down to the bottom of the scroll view, I suspect it's related to your choice of
scale
of5
for yourUIGraphicsBeginImageContextWithOptions
. Usually you use1
(non-retina),2
(retina), or0
(use the scale from the main screen).As an aside, you don't need the
__block
qualifier for yourCGContextRef
. You can access thecontext
variable without it.