cgcontext

Eraser the drawing in iOS

柔情痞子 提交于 2019-12-02 11:43:11
I am working on a drawing app, I have a UIBezeirPath, with which I draw in touchesMoved, and convert it to CGPath and then draw on to CGlayer, Here is my code -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { self.currentPath = [[DrawingPath alloc] init]; if(m_eraseButtonClicked) { [self.currentPath setPathColor:[UIColor backgroundColor]]; } else { [self.currentPath setPathColor:self.lineColor]; } CGPathRef cgPath = self.currentPath.path.CGPath; mutablePath = CGPathCreateMutableCopy(cgPath); } - (void)Erase { CGContextRef layerContext = CGLayerGetContext(self.DrawingLayer);

Masking an image using bezierpath with image's full resolution

二次信任 提交于 2019-12-02 04:41:18
Hi, I have a path (shape) and a high-resolution image. I make the high res image to be AspectFit inside the view on which I draw the path and I want to mask the image with the path but at the full resolution of the image, not at the resolution in which we see the path. The problem, It works perfectly when I don't upscale them up for high-resolution masking but when I do, everything is messed up. The mask gets stretched and the origins don't make sense. All I want is to be able to upscale the path with the same aspect ratio of the image (at the full resolution of the image) and position it

Number of alpha pixels in CGContext

我们两清 提交于 2019-12-01 14:38:51
I have a masking CGContext with 2 types of pixels: color and alpha (opaque and transparent pixels). How to calculate percentage of alpha pixels in my context? Jeshua Lacock I didn't test it, but this should do the trick - just pass ReportAlphaPercent a CGImageRef: UIImage *foo; //The image you want to analyze float alphaPercent = ReportAlphaPercent(foo.CGImage); float ReportAlphaPercent(CGImageRef imgRef) { size_t w = CGImageGetWidth(imgRef); size_t h = CGImageGetHeight(imgRef); unsigned char *inImage = malloc(w * h * 4); memset(inImage, 0, (h * w * 4)); CGColorSpaceRef colorSpace =

iOS Draw Rectagle with curved ends

女生的网名这么多〃 提交于 2019-12-01 13:31:36
Imagine a long rectangle (maybe with size 200x20). All sides have straight edges. In my iOS app, this is easy for me to draw: CGContextFillRect(context, CGRectMake(xLoc, yLoc, 200, 20)); Now what if I wanted the shorter ends (on the left and right side of the rectangle) to be slightly curved, rather than straight edges. What would the code look like to do this? (Note that I am relatively inexperienced with CGContext drawing) Something like this (untested, so beware of bugs!): - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor

Number of alpha pixels in CGContext

寵の児 提交于 2019-12-01 12:56:23
问题 I have a masking CGContext with 2 types of pixels: color and alpha (opaque and transparent pixels). How to calculate percentage of alpha pixels in my context? 回答1: I didn't test it, but this should do the trick - just pass ReportAlphaPercent a CGImageRef: UIImage *foo; //The image you want to analyze float alphaPercent = ReportAlphaPercent(foo.CGImage); float ReportAlphaPercent(CGImageRef imgRef) { size_t w = CGImageGetWidth(imgRef); size_t h = CGImageGetHeight(imgRef); unsigned char *inImage

iOS Draw Rectagle with curved ends

穿精又带淫゛_ 提交于 2019-12-01 11:53:55
问题 Imagine a long rectangle (maybe with size 200x20). All sides have straight edges. In my iOS app, this is easy for me to draw: CGContextFillRect(context, CGRectMake(xLoc, yLoc, 200, 20)); Now what if I wanted the shorter ends (on the left and right side of the rectangle) to be slightly curved, rather than straight edges. What would the code look like to do this? (Note that I am relatively inexperienced with CGContext drawing) 回答1: Something like this (untested, so beware of bugs!): - (void

CGBitmapContextCreate on the iPhone/iPad

落花浮王杯 提交于 2019-12-01 05:16:13
I have a method that needs to parse through a bunch of large PNG images pixel by pixel (the PNGs are 600x600 pixels each). It seems to work great on the Simulator, but on the device (iPad), i get an EXC_BAD_ACCESS in some internal memory copying function. It seems the size is the culprit because if I try it on smaller images, everything seems to work. Here's the memory related meat of method below. + (CGRect) getAlphaBoundsForUImage: (UIImage*) image { CGImageRef imageRef = [image CGImage]; NSUInteger width = CGImageGetWidth(imageRef); NSUInteger height = CGImageGetHeight(imageRef);

Drawing a dashed line with CGContextSetLineDash

谁说胖子不能爱 提交于 2019-12-01 04:46:45
I'm trying to draw a dashed line with CGContextSetLineDash . Here's my code: float dashPhase = 0.0; float dashLengths[] = {30, 30}; CGContextSetLineDash(context, dashPhase, dashLengths, 20.0); self.previousPoint2 = self.previousPoint1; self.previousPoint1 = previous; self.currentPoint = current; self.mid1 = [self pointBetween:self.previousPoint1 andPoint:self.previousPoint2]; self.mid2 = [self pointBetween:self.currentPoint andPoint:self.previousPoint1]; UIBezierPath* newPath = [UIBezierPath bezierPath]; [newPath moveToPoint:self.mid1]; [newPath addLineToPoint:self.mid2]; [newPath setLineWidth

Using CGContext to draw line

假装没事ソ 提交于 2019-12-01 04:44:13
I wanted to draw a line using CGContext and what I have so far is: CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor); CGContextSetLineWidth(context, 1.0f); CGContextMoveToPoint(context, 10, 10); CGContextAddLineToPoint(context, 100, 50); CGContextStrokePath(context); It always draws from the top left corner to the top bottom right corner. How can I adjust the start and end origin of this line? How do I adjust the length of the line? These two lines are responsible for the start and end points: CGContextMoveToPoint

CGBitmapContextCreate on the iPhone/iPad

守給你的承諾、 提交于 2019-12-01 02:42:16
问题 I have a method that needs to parse through a bunch of large PNG images pixel by pixel (the PNGs are 600x600 pixels each). It seems to work great on the Simulator, but on the device (iPad), i get an EXC_BAD_ACCESS in some internal memory copying function. It seems the size is the culprit because if I try it on smaller images, everything seems to work. Here's the memory related meat of method below. + (CGRect) getAlphaBoundsForUImage: (UIImage*) image { CGImageRef imageRef = [image CGImage];