CGContextDrawImage when shifting x offset - iphone

So I have the following code:
CGContextDrawImage(context, CGRectMake(100, 0, 220, self.image.size.height), self.image.CGImage);
basically I am asking CG to draw the image 100 px to the right.. now this works fine.. however to the left of the image I see a white background. How do I change this background to some other color? Say I want a black blackground

Before drawing the image, fill the rect with the desired color:
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextFillRect(context, CGRectMake(0, 0, self.image.size.width + 100, self.image.size.height)); //I suppose your rect's width is as much as image's width +100
//Then draw your image
CGContextDrawImage(context, CGRectMake(100, 0, 220, self.image.size.height), self.image.CGImage);
Note: Make sure the rect's you're drawing into width is at least the image's width + 100

Related

iPhone - draw a transparent (cleared) rectangle with customized border

I'm trying to draw a rectangle, which should have black color border of width 5.0, I am getting the rectangle as seen below,
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextStrokePath(context);
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 0.5);
CGContextFillRect(context, rect);
I can make it clear / transparent (white) background instead the green one showing right now with [UIColor whiteColor].CGColor but then it should have black border also.
How do I set the customized border to rectangle?
Set the stroke color and width as desired, for example:
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetLineWidth(context, 5.0f);
CGContextStrokeRect(context, rect);
If you are also filling the rectangle, do this after filling so the fill doesn't cover up the stroke.

Stroke is being applied to both Text and Shadow

I'm trying to simply draw some text with a stroke and then apply a drop shaddow, but the stroke is being applied to drop shadow. How do I prevent this?
// Setup context
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
// draw photo into context
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
// Set Text Font
CGContextSelectFont(context, font.fontName.UTF8String, fontSize, kCGEncodingMacRoman);
// Set Text Drawing Mode
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
// Set text fill color
CGColorRef tmpColor = color.CGColor;
CGFloat newComponents[4] = {};
memcpy(newComponents, CGColorGetComponents(tmpColor), sizeof(newComponents));
CGContextSetRGBFillColor(context, newComponents[0], newComponents[1], newComponents[2], newComponents[3]);
// Calculate Height
UIFont *testFont = [UIFont fontWithName:font.fontName size:fontSize];
CGSize size = [text sizeWithFont:testFont];
// Setup Font Shadow
CGSize shadowSize = CGSizeMake(6, 6);
CGContextSetShadowWithColor(context, shadowSize, 1.0, [[UIColor darkGrayColor] CGColor]);
// Set Stroke Color
CGContextSetRGBStrokeColor(context, 0, 255, 0, 1);
CGContextSetLineWidth(context, 2.0);
// draw text at location centered based on width.
CGContextShowTextAtPoint(context, (w / 2) - (textWidth / 2), h - size.height, ctext, strlen(ctext));
// Render Bitmap
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
UIImage *returnImage = [UIImage imageWithCGImage:imageMasked];
Because your text drawing mode is kCGTextFillStroke, CGContextShowTextAtPoint is first drawing the fill (and generating a shadow for it), and then drawing the stroke (which gets its own shadow).
To fix it, use a transparency layer.
Note that it will draw much faster if you use CGContextBeginTransparencyLayerWithRect() and pass in as small a rect as you can.
Alternatively: if it's a good enough approximation, you might consider drawing your text in two steps:
fill, with shadow
stroke, with no shadow
If your stroke is small, the difference will not be very perceptible, and it will draw considerably faster.

clearing screen after CGContextDrawImage

I am trying for a image relaterd i-phone application.I am using a line of code
CGContextDrawImage(context1, CGRectMake(0, 0, width, height), imageRef);
I am working with multiple images.So i want to clear the screen after this .Any built in function using for this (Like in C we can use cleardevice in graphics).Any help appreciating.
Check out CGContextFillRect - just set the rect to be the whole view and the brush to be solid white :)
For example, to clear the image you just drew with white :
const CGFloat white[] = {1.0f, 1.0f, 1.0f, 1.0f};
CGContextSetFillColor(context1, white);
CGContextFillRect(context1, CGRectMake(0, 0, width, hight));

Core Graphics - drawing a line at an angle

I've been searching struggling with this for a while now and need some help. I just want to draw 25 lines from the centre of my display (160,200) at 14.4 degrees separation. I have been using this line of code in a for loop where x is the 14.4 degree multiplier -
UIImage*backgroundImage = [UIImage imageNamed:#"Primate Background Only.png"];
[backgroundImage drawInRect:CGRectMake(0, 0, 320, 480)];
// Draw Outer Circle
rect = CGRectMake(0.0, 35.0, 320.0, 320.0);
CGContextRef contextRef = UIGraphicsGetCurrentContext(); // Get the contextRef
CGContextSetLineWidth (contextRef, 0.5); // Set the border width
CGContextSetRGBFillColor (contextRef, (219.0f/255.0f), (219.0f/255.0f), (219.0f/255.0f), 0.05f); // Set the circle fill color to GREEN
CGContextSetRGBStrokeColor (contextRef, 0.0, 0.0, 0.0, 0.2); // Set the circle border color to BLACK
CGContextFillEllipseInRect (contextRef, rect); // Fill the circle with the fill color
CGContextStrokeEllipseInRect (contextRef, rect); // Draw the circle border
// Draw Inner Circle
rect = CGRectMake(25.0, 60.0, 270.0, 270.0); // Get the contextRef
CGContextSetLineWidth (contextRef, 0.5); // Set the border width
CGContextSetRGBFillColor (contextRef, (219.0f/255.0f), (219.0f/255.0f), (219.0f/255.0f), 0.25f);
CGContextSetRGBStrokeColor (contextRef, 0.0, 0.0, 0.0, 0.2); // Set the circle border color to BLACK
CGContextFillEllipseInRect (contextRef, rect); // Fill the circle with the fill color
CGContextStrokeEllipseInRect (contextRef, rect);
// Draw Segments
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.0, 0.0);
for (x=1; x<26; x++) {
CGContextSetLineWidth (context, 0.5);
CGContextSetRGBStrokeColor (context, 0.0, 0.0, 0.0, 0.25);
CGContextMoveToPoint (context, 160, 200);
CGContextAddLineToPoint (context, (160.0 * (cos((x*14.4)*(M_PI/180)))), (160.0 * (sin((x*14.4)*(M_PI/180))))); // The angle is in degrees
CGContextAddLineToPoint (context, 200, 65);
CGContextAddLineToPoint (context, 160, 200);
CGContextStrokePath(context); // Why is this not showing both line color and infill?
CGContextSetFillColorWithColor (context, [UIColor whiteColor].CGColor);
CGContextFillPath (context);
CGContextRef context = UIGraphicsGetCurrentContext();
}
(I intended to post an image here but it won't permit me!)
Can someone please correct my trig functions. This is meant to draw 25 lines clockwise from 12:00 o'clock to 24:00. Instead it draws backwards only 90 degrees then returns, all lines are way out in length also.
Very grateful
Above is a bigger sample of the code used to draw tow outer circles and the interior segments as requested. I'll try and upload the image next.
Your main problem is this line of code:
CGContextAddLineToPoint(context, (160.0 * (cos((x*14.4)*(M_PI/180)))),
(160.0 * (sin((x*14.4)*(M_PI/180)))));
it should be:
CGContextAddLineToPoint(context, 160 + (160.0 * (cos((x*14.4)*(M_PI/180)))),
200 + (160.0 * (sin((x*14.4)*(M_PI/180)))));
As you had originally written it the lines were be drawn relative to 0,0 but you want to draw relative to your center point (160, 200).
The next 2 lines are also a little suspect:
CGContextAddLineToPoint (context, 200, 65);
CGContextAddLineToPoint (context, 160, 200);
It is not clear to me what you were trying to do here, but this would end up drawing a line from the end of each arm to a fixed point (200,65) and from there back to you center point, which is almost certainly not what you intended! Try commenting out these lines, confirm that the "arms" are being drawn correctly and take it from there...
Hope this is clear
If you make these corrections your screen will look something like this:

Drawing a rounded drop shadow with Quartz

I am trying to draw a drop shadow underneath an image to which I apply rounded corners, but I have 2 problems:
The drop shadow only appears underneath the non-rounded area of the rounded image and not underneath the bottom rounded corners as one would get if applying a drop shadow in photoshop.
Using the same settings as in photoshop (a 2 y-axis offset, 1 blur and 85% black) results in a much darker shadow which does not appear as blurred as it should.
Any help would be appreciated please.
float myColorValues[] = {0, 0, 0, 0.85};
CGColorRef myColor = CGColorCreate(colorSpace, myColorValues);
CGContextSetShadowWithColor(context, CGSizeMake(0, -2), 2, myColor);
// Draw a round corner path
CGContextBeginPath(context);
CGRect rect = CGRectMake(0, 0, 68, 68);
addRoundedRectToPath(context, rect, cornerWidth, cornerHeight);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawImage(context, CGRectMake(1, 2, 70, 70), imageScaledAndCropped);
The solution is to draw a rounded bezier path underneath the image and to apply the shadow to that.