cgcontext

iOS to Mac GraphicContext Explanation/Conversion

泪湿孤枕 提交于 2019-11-29 07:36:43
问题 I have been programming for two years on iOS and never on mac. I am working on a little utility for handling some simple image needs that I have in my iOS development. Anyway, I have working code in iOS that runs perfectly but I have absolutely no idea what equivalents are for mac. I've tried a bunch of different things but I really don't understand how to start a graphics context on the Mac outside of a "drawRect:" method. On the iPhone I would just use UIGraphicsBeghinImageContext(). I know

Draw a simple circle uiimage

て烟熏妆下的殇ゞ 提交于 2019-11-29 00:21:39
问题 I try to make a 20x20 UIImage with a simple blue circle. I try with this function, but the result is a blue circle in a black square. How do I remove the black square around the circle? Function: + (UIImage *)blueCircle { static UIImage *blueCircle = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ UIGraphicsBeginImageContextWithOptions(CGSizeMake(20.f, 20.f), YES, 0.0f); CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSaveGState(ctx); CGRect rect = CGRectMake(0,

Create a UIImage by rendering UIWebView on a background thread - iPhone

℡╲_俬逩灬. 提交于 2019-11-28 23:46:10
Does someone know of a way, or has a creative idea as to how to obtain an UIImage that is rendered from a UIWebView? The catch is, that is must be on a background thread. I'll elaborate: I'm trying to obtain the image from multiple UIWebViews every second+-, and display it on screen (iPhone's of course). Because rendering a layer to a CGContext is a CPU consuming job, I wouldn't like to use the main thread, so not to hang the UI. My attempts so far were: I tried to use renderInContext on the webView's layer to the UIGraphicalContext , but the _WebTryThreadLock error crashed the webviews. I

Using CGContext to display a sprite sheet iPhone

送分小仙女□ 提交于 2019-11-28 22:11:00
So I have a spritesheet in png format, and have already worked out the coordinates for what I want to display. I'm trying to create a method that will return a UIImage when passed the location information on the spritesheet. I'm just not sure how to use the CGContext stuff along with the the coordinates to return an UIImage. I was looking to CGContextClipToRect because I think that is the way to do it, but I just can't seem to get it to work. Something like this CGSize size = CGSizeMake(xSize, ySize); UIGraphicsBeginImageContext(size); //CGContextClipToRect(spriteSheet.size, imageRect);

iOS draw filled Circles

核能气质少年 提交于 2019-11-28 16:41:48
Not a graphics programmer here, so I'm trying to stumble through this. I'm trying to draw 9 filled circles, each a different color, each with a white border. The UIView's frame is CGRectMake (0,0,60,60). See attached image. The problem is I'm getting "flat spots" on the borders on each side. Following is my code (from the UIView subclass): - (void)drawRect:(CGRect)rect { CGRect borderRect = CGRectMake(0.0, 0.0, 60.0, 60.0); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); CGContextSetRGBFillColor(context, colorRed, colorGreen,

Calculate controlPoints while drawing in iOS

旧街凉风 提交于 2019-11-28 12:49:40
I am working on a drawing app, where user can draw/write with his finger or stylus. For this I have referred code from https://github.com/yusenhan/Smooth-Line-View , in my application. The problem which I am facing is that, the writing sometimes when you write very closely is not very smooth. So I think, I am making some mistake in getting the control point. Below is my code //Find the midpoint CGPoint midPoint(CGPoint p1, CGPoint p2) { return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5); } #pragma mark Gesture handle -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

rotate image using CGContextDrawImage

夙愿已清 提交于 2019-11-28 12:08:42
How can I rotate an image drawn by CGContextDrawImage() at its center? In drawRect: CGContextSaveGState(c); rect = CGRectOffset(rect, -rect.size.width / 2, -rect.size.height / 2); CGContextRotateCTM(c, angle); CGContextDrawImage(c, rect, doodad.CGImage); CGContextRotateCTM(c, -angle); CGContextTranslateCTM(c, pt.x, pt.y); prevRect = rect; CGContextRestoreGState(c); I draw the image at the origin, and rotate it at its center, then translate to where it should be drawn. Not working. The thing to remember about coordinate transformations—well, one of the things to remember—is that you are not

Drawing a polygon with one color for stroke, and a different one for fill?

烈酒焚心 提交于 2019-11-28 08:45:31
I'm having trouble with drawing some lines that are stroked with a color and then filling their insides (they make a polygon) with another one. UIColor *houseBorderColor = [UIColor colorWithRed:(170/255.0) green:(138/255.0) blue:(99/255.0) alpha:1]; CGContextSetStrokeColorWithColor(context, houseBorderColor.CGColor); CGContextSetLineWidth(context, 3); // Draw the polygon CGContextMoveToPoint(context, 20, viewHeight-19.5); CGContextAddLineToPoint(context, 200, viewHeight-19.5); // base CGContextAddLineToPoint(context, 300, viewHeight-119.5); // right border CGContextAddLineToPoint(context, 120,

CGBitmapContextCreate error with swift

匆匆过客 提交于 2019-11-28 08:05:24
I am trying to create a CGContext in swift. It compiles but throws an error at runtime. let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB() let context:CGContext = CGBitmapContextCreate(nil, 20, 20, 8, 0, colorSpace, CGBitmapInfo.AlphaInfoMask) CGColorSpaceRelease(colorSpace); .... And the error is: Error: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; unrecognized; 96 bytes/row. fatal error: Can't unwrap Optional.None loopmasta Just in case somebody is running into the same problem. The snippet below finally

Convert Image to B&W problem CGContext - iPhone Dev

元气小坏坏 提交于 2019-11-28 07:04:45
I am using a method to convert images taken with the camera into black and white. The problem I'm running into is that if the picture is taken in Portrait mode, it rotates and stretches in the next view. But, if is taken in landscape it is just find. I can only duplicate this bug when I convert the image to Black And White. Here is the method I am using. Im not 100% that this bug is from the conversion, but it only happens when its converted. Oh ya. This is the iOS 4. /// - (UIImage *)convertImageToGrayScale:(UIImage *)image { // Create image rectangle with current image width/height CGRect