I have this code on my touchesMoved but like the others its not working on UIScrollView
Here it is, on my touchesMoved:
touchSwiped = YES;
currentTouch = [touch locationInView:self.view];
currentTouch.y -= 5;
UIGraphicsBeginImageContext(self.view.frame.size);
[writeView.image drawInRect:CGRectMake(0, 0, 768, 1024)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 15);
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextMoveToPoint(context, endPoint.x, endPoint.y);
CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
CGContextStrokePath(context);
writeView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
endPoint = currentTouch;
touchMoved++;
if (touchMoved == 10) {
touchMoved = 0;
}
so, i transferred it using Gesture recognizer but still it is not working.
i used the PanGestureRecognizer
here:
- (void) writePan:(UIPanGestureRecognizer *)writingRecognizerP {
switch (writingRecognizerP.state)
{
case UIGestureRecognizerStateChanged:
[scrollView setScrollEnabled:NO];
[scrollView setUserInteractionEnabled:NO];
touchSwiped = YES;
currentTouch = [writingRecognizerP locationInView:scrollView];
currentTouch.y -= 5;
UIGraphicsBeginImageContext(self.view.frame.size);
[writeView.image drawInRect:CGRectMake(0, 0, 768, 1024)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 15);
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextMoveToPoint(context, endPoint.x, endPoint.y);
CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
CGContextStrokePath(context);
writeView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
endPoint = currentTouch;
touchMoved++;
if (touchMoved == 10) {
touchMoved = 0;
}
break;
case UIGestureRecognizerStateEnded:
[scrollView setScrollEnabled:YES];
[scrollView setUserInteractionEnabled:YES];
break;
}
}
anyone who has an idea how will i able to write on touch??
it'll be much appreciated! :)
You should do all your drawing in -drawRect:, and when something changes send your view -setNeedsDisplay or setNeedsDisplayInRect: message.
Related
I'm a little bit nooby in Objective-C and I just started working with Quartz 2D. My problem is very simple: I want to draw in Quartz 2D by pressing button. I don't know how I can do that because there are two different classes in my project. I used that tutorial: http://www.techotopia.com/index.php/An_iOS_7_Graphics_Tutorial_using_Core_Graphics_and_Core_Image
Here is my code:
ViewController.h
#interface ViewController : UIViewController
- (IBAction)Start;
ViewController.m
- (IBAction)Start {
}
Draw2D.h
#import <UIKit/UIKit.h>
#interface Draw2D : UIView
#end
Draw2D.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextMoveToPoint(context, 30, 30);
CGContextAddLineToPoint(context, 300, 400);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 4.0);
CGContextSetStrokeColorWithColor(context,
[UIColor blueColor].CGColor);
CGRect rectangle = CGRectMake(60,170,200,80);
CGContextAddRect(context, rectangle);
CGContextStrokePath(context);
}
If you want to show Draw2D view upon pressing a button, you can add draw2DView as subview of your ViewController:
/// ViewController
- (IBAction)Start:(UIButton *)sender
{
[self.view addSubview:draw2DView];
}
and you should remove these lines in your Draw2D initWithFrame method
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextMoveToPoint(context, 30, 30);
CGContextAddLineToPoint(context, 300, 400);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
They are below return self; therefore will not be triggered.
I'm trying to draw a circle for my Mac Application. The Code is:
- (void)mouseMoved:(NSEvent*)theEvent {
NSPoint thePoint = [[self.window contentView] convertPoint:[theEvent locationInWindow] fromView:nil];
NSLog(#"mouse moved: %f % %f",thePoint.x, thePoint.y);
CGRect circleRect = CGRectMake(thePoint.x, thePoint.y, 20, 20);
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetRGBFillColor(context, 0, 0, 255, 1.0);
CGContextSetRGBStrokeColor(context, 0, 0, 255, 0.5);
CGContextFillEllipseInRect(context, CGRectMake(circleRect.origin.x, circleRect.origin.y, 25, 25));
CGContextStrokeEllipseInRect(context, circleRect);
[self needsDisplay];
}
- (void)mouseMoved: is called perfectly and I can see the correct x and y coordinates in NSLog. But I do not get any circles... surprisingly: If I'm minimizing my application and reopening it (so it "updates" NSView) the circles are perfectly drawn!
mouseMoved is NOT right place to draw anything, unless you're drawing onto the offscreen buffer. If you are going to draw on screen, save thePoint and any other necessary data, call [self setNeedsDisplay:YES] and draw in the drawRect:(NSRect)rect method.
Also, I can't see a reason to use CGContextRef while there is much more "friendly" NSGraphicsContext. Although, it's matter of taste.
An example of the drawing code:
- (void)mouseMoved:(NSEvent*)theEvent {
// thePoint must be declared as the class member
thePoint = [[self.window contentView] convertPoint:[theEvent locationInWindow] fromView:nil];
[self setNeedsDisplay:YES];
}
- (void)drawRect:(NSRect)rect
{
NSRect ovalRect = NSMakeRect(thePoint.x - 100, thePoint.y - 100, 200, 200);
NSBezierPath* oval = [NSBezierPath bezierPathWithOvalInRect:ovalRect];
[[NSColor blueColor] set];
[oval fill];
[[NSColor redColor] set];
[oval stroke];
}
I have following problem:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineCap(context,kCGLineCapRound);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetRGBFillColor(context, 1, 0, 0, 1);
for (drawnCurve *line in completeLines) {
CGContextBeginPath(context);
for(int i=0;i<[line.points count]-1;i++)
{
drawnPoint*point=[line.points objectAtIndex:i];
drawnPoint*point2=[line.points objectAtIndex:i+1];
// CGContextSetLineWidth(context, point.r);
[point.color set];
CGContextMoveToPoint(context, point.x,point.y);
CGContextAddLineToPoint(context, point2.x, point2.y);
// CGContextAddCurveToPoint(context, (point.x+point2.x)/2, point2.y, (point.x+point2.x)/2, point2.y, point2.x, point2.y);
//CGContextEOFillPath(context);
}
CGContextClosePath(context);
CGContextFillPath(context);
}
But it's just disappearing and not showing any kind of filled curves.. What is the problem?
A call to CGContextMoveToPoint begins a new subpath. You should call it once before your nested for loop, and then use only CGContextAddLineToPoint to preserve line connectivity:
drawnPoint*pointZero=[line.points objectAtIndex:0];
CGContextMoveToPoint(context, pointZero.x,pointZero.y);
for(int i=1;i<[line.points count];i++)
{
drawnPoint*point=[line.points objectAtIndex:i];
CGContextAddLineToPoint(context, point.x, point.y);
}
CGContextClosePath(context);
CGContextFillPath(context);
This question already has answers here:
Assigning an existing CGColor to a CGColor property works in iOS Simulator, not iOS device. Why?
(3 answers)
Closed 9 years ago.
I have a custom UIButton class that adds a gradient and a gloss effect to the UIButton
the code works perfectly in iOS 4 and on iOS5 simulator but when i run it on iOS 5 devices it gives me the exception EXC_BAD_ACCESS , the exception is triggered by the line :
CGContextStrokePath(context);
any help is really appreciated ,
here's my code
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat actualBrightness = _brightness;
if (self.selected) {
actualBrightness -= 0.10;
}
CGColorRef blackColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0].CGColor;
CGColorRef highlightStart = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.7].CGColor;
CGColorRef highlightStop = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0].CGColor;
CGColorRef outerTop = [UIColor colorWithHue:_hue saturation:_saturation brightness:1.0*actualBrightness alpha:1.0].CGColor;
CGColorRef outerBottom = [UIColor colorWithHue:_hue saturation:_saturation brightness:0.80*actualBrightness alpha:1.0].CGColor;
CGFloat outerMargin = 7.5f;
CGRect outerRect = CGRectInset(self.bounds, outerMargin, outerMargin);
CGMutablePathRef outerPath = createRoundedRectForRect(outerRect, 6.0);
// Draw gradient for outer path
CGContextSaveGState(context);
CGContextAddPath(context, outerPath);
CGContextClip(context);
drawLinearGradient(context, outerRect, outerTop, outerBottom);
CGContextRestoreGState(context);
if (!self.selected) {
CGRect highlightRect = CGRectInset(outerRect, 1.0f, 1.0f);
CGMutablePathRef highlightPath = createRoundedRectForRect(highlightRect, 6.0);
CGContextSaveGState(context);
CGContextAddPath(context, outerPath);
CGContextAddPath(context, highlightPath);
CGContextEOClip(context);
drawLinearGradient(context, CGRectMake(outerRect.origin.x, outerRect.origin.y, outerRect.size.width, outerRect.size.height/3), highlightStart, highlightStop);
CGContextRestoreGState(context);
drawCurvedGloss(context, outerRect, 180);
CFRelease(highlightPath);
}
else {
//reverse non-curved gradient when pressed
CGContextSaveGState(context);
CGContextAddPath(context, outerPath);
CGContextClip(context);
drawLinearGloss(context, outerRect, TRUE);
CGContextRestoreGState(context);
}
if (!_toggled) {
//bottom highlight
CGRect highlightRect2 = CGRectInset(self.bounds, 6.5f, 6.5f);
CGMutablePathRef highlightPath2 = createRoundedRectForRect(highlightRect2, 6.0);
CGContextSaveGState(context);
CGContextSetLineWidth(context, 0.5);
CGContextAddPath(context, highlightPath2);
CGContextAddPath(context, outerPath);
CGContextEOClip(context);
drawLinearGradient(context, CGRectMake(self.bounds.origin.x, self.bounds.size.height-self.bounds.size.height/3, self.bounds.size.width, self.bounds.size.height/3), highlightStop, highlightStart);
CGContextRestoreGState(context);
CFRelease(highlightPath2);
}
else {
//toggle marker
CGRect toggleRect= CGRectInset(self.bounds, 5.0f, 5.0f);
CGMutablePathRef togglePath= createRoundedRectForRect(toggleRect, 8.0);
CGContextSaveGState(context);
CGContextSetLineWidth(context, 3.5);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextAddPath(context, togglePath);
CGContextStrokePath(context);
CGContextRestoreGState(context);
CFRelease(togglePath);
}
// Stroke outer path
CGContextSaveGState(context);
CGContextSetLineWidth(context, 0.5);
CGContextSetStrokeColorWithColor(context, blackColor);
CGContextAddPath(context, outerPath);
CGContextStrokePath(context);
CGContextRestoreGState(context);
CFRelease(outerPath);
}
what I really wanna now is that is it really related to iOS 5 or am i doing something else wrong ?
The crash happens because the UIColors are already deallocated when you access the CGColorRefs.
An easy way to avoid this is to use
UIColor* blackColor = [UIColor blackColor];
CGContextSetStrokeColorWithColor(context, [blackColor CGColor]);
instead of
CGColorRef* blackColor = [[UIColor blackColor] CGColor];
CGContextSetStrokeColorWithColor(context, blackColor);
so ARC doesn't get the chance to deallocate the UIColor objects early.
I try to Draw a pdf with CoreGraphics, everything work fine but in instrument there is a 100% leak on : CGContextDrawPDFPage(ctx, page2);
I release with CGPDFDocumentRelease(); everytime i use CGPDFDocumentCreateWithURL();
There is any solution to release : CGContextDrawPDFPage ?
- (void)drawRect:(CGRect)rect
{
if (state == 0) {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
[[UIColor whiteColor] set];
CGContextFillRect(ctx, CGRectMake(0, 0, rect.size.width, rect.size.height));
CGContextGetCTM(ctx);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 45, -rect.size.height);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(((CFURLRef)url));
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, currentPage);
CGRect mediaRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
CGContextScaleCTM(ctx, (678) / mediaRect.size.width,
(rect.size.height ) / (mediaRect.size.height));
CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);
CGContextDrawPDFPage(ctx, page);
CGPDFDocumentRelease(pdf);
CGContextRestoreGState(ctx);
}
else
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
[[UIColor whiteColor] set];
CGContextFillRect(ctx, CGRectMake(0, 0, rect.size.width, rect.size.height));
CGContextGetCTM(ctx);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 6, -rect.size.height);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(((CFURLRef)url));
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, currentPage);
CGRect mediaRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
CGContextScaleCTM(ctx, (497) / mediaRect.size.width,
(rect.size.height ) / (mediaRect.size.height));
// draw it
CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);
CGContextDrawPDFPage(ctx, page);
CGPDFDocumentRelease(pdf);
CGContextRestoreGState(ctx);
//
CGContextSaveGState(ctx);
CGContextGetCTM(ctx);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 506, -rect.size.height);
CGPDFDocumentRef pdf2 = CGPDFDocumentCreateWithURL(((CFURLRef)url));
CGPDFPageRef page2 = CGPDFDocumentGetPage(pdf2, (currentPage + 1));
CGContextScaleCTM(ctx, (497) / mediaRect.size.width,
(rect.size.height ) / (mediaRect.size.height));
CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);
//Leak 100%
CGContextDrawPDFPage(ctx, page2);
CGPDFDocumentRelease(pdf2);
CGContextRestoreGState(ctx);
}
}
And I don't know why. Any idea?
This is the only leak of my app :(
I don't see where the leak come from :s
PS : state = 0 = portrait orientation
state = 1 = landscape so I draw 2 pages in landscape orientation.
Looks like you're not releasing the CGPDFPageRef instances you're getting from CGPDFDocumentGetPage.
CGPDFPageRelease(page);
Add that for page and page2 at the appropriate locations.