I'm writing on an image using:
float shadowColorValues[] = {0, 0, 0, .3};
CGContextSetShadowWithColor(context, CGSizeMake (2, -2), 3 * imageScale, shadowColor);
CGContextSelectFont(context, newFontName, fontSize, kCGEncodingMacRoman);
CGContextSetRGBFillColor(context, 255, 255, 255, 1);
CGContextSetRGBStrokeColor(context, 255, 255, 255, 1);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetTextMatrix(context, CGAffineTransformMake(1.0, 0.0, 0.0, 1.0, 0.0, 0.0));
CGContextShowTextAtPoint(context, textX, textY, newText, strlen(newText));
If I'm using a font like Chalkduster the result is very pixelated: http://i.imgur.com/WooNB.png. What am I doing wrong?
Make sure to use
void UIGraphicsBeginImageContextWithOptions(
CGSize size,
BOOL opaque,
CGFloat scale // pass 0 for screen scale
);
when creating the image context.
Related
I have created a Google map view where I am setting markers on their respective positions and and adding some text over them. Now I want to change size of that text as per zoom, means that size should be bigger on zoom out and lesser on map zoom in. How to achieve the same ? I tried different logic but failed. I am creating that marker text using following function,
// Add text to UIImage
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1 red:(int)red green:(int)green blue:(int)blue alpha:(float)Alpha{
int w = img.size.width;
int h = img.size.height;
//lon = h - lon;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = (CGBitmapInfo) kCGImageAlphaPremultipliedFirst;
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, bitmapInfo);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
char * text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
//Here I set size of text
CGContextSelectFont(context, "HelveticaNeue-Bold", 15, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetTextDrawingMode(context, kCGTextInvisible);
CGContextShowTextAtPoint(context, 0, 0, text, strlen(text));
// Then get the position of the text and set the mode back to visible:
CGPoint pt = CGContextGetTextPosition(context);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 0, 0, 0, 1); // black - for shadow
CGContextShowTextAtPoint(context, (w/2) - (pt.x / 2) + 1, h/2, text, strlen(text));
CGContextSetRGBFillColor(context, red, green, blue, Alpha); // white - for text
CGContextShowTextAtPoint(context, (w/2) - (pt.x / 2), h/2, text, strlen(text));
CGImageRef imageMasked=nil;
imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
} // addText
This question already has answers here:
How to draw a rounded rectangle in Core Graphics / Quartz 2D?
(9 answers)
Closed 8 years ago.
The code below in my drawRect method draws a rectangle on my view controller. I'd like to set a corner radius to this. What am I missing?
CGRect rectangle = CGRectMake(20, 22, 280, 460);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 255, 255, 255, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextFillRect(context, rectangle);
Try following, please:
CGRect rectangle = CGRectMake(20, 22, 280, 460);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 255, 255, 255, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
//CGContextFillRect(context, rectangle);
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect: rectangle cornerRadius:15.0];
[bezierPath fill];
Need to take photo from the application with date and time the photo has been taken to be displayed in photo just like digital camera.
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1;
{
int w = img.size.width;
int h = img.size.height;
//lon = h - lon;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];// "05/05/09";
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 255, 255, 255, 1);
//rotate text
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));
CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
I'm going to write a texture atlas manager, but I've run into a problem. When I modified my original UV coordinates, my program no longer rendered anything?? This is my draw code:
- (void)drawFrame {
[(EAGLView *)self.view setFramebuffer];
//GLfloat aspectRatio = self.view.bounds.size.height/self.view.bounds.size.width;
float m[16] = {2/self.view.bounds.size.width, 0, 0, 0, 0, 2/self.view.bounds.size.height, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1 };
CGSize imageSize = CGSizeMake(512, 512);
CGRect drawBounds = CGRectMake(0, 0, 106, 126);
GLfloat x = (1/imageSize.width) * drawBounds.size.width;
GLfloat y = (1/imageSize.height) * drawBounds.size.height;
static const GLfloat vertices[] = {
-53.0f, -63.0f,
53.0f, -63.0f,
-53.0f, 63.0f,
53.0f, 63.0f,
};
GLfloat texCoords[] = {
0.0, 0.0,
x, 0.0,
0.0, y,
x, y
};
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
[[TextureLibrary sharedTextureLibrary] bindTexture:#"bounce" toSlot:GL_TEXTURE0];
glUniform1f(uniforms[UNIFORM_TEXTURE], 0);
glUniformMatrix4fv(UNIFORM_MVP_MATRIX, 1, GL_FALSE, m);
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, vertices);
glEnableVertexAttribArray(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_TEXTURE_POSITION, 2, GL_FLOAT, 0, 0, texCoords);
glEnableVertexAttribArray(ATTRIB_TEXTURE_POSITION);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
[(EAGLView *)self.view presentFramebuffer];
}
However, it works fine like this:
GLfloat texCoords[] = {
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
1.0, 1.0
};
Help?
Just a guess, but OpenGL considers (0, 0) to be the lower left corner of an image. Is it possible your graphic is in the top left, so that the area you're displaying is actually from a transparent part of the texture?
If so then you can either adjust your texture coordinates or adjust the texture matrix stack so that (0, 0) is in the top left.
I want to draw text use Quartz 2D. The "menu" direction is wrong. I want "Menu" still readable and have 45 degree with X-axis. Below is my code:
CGContextSelectFont(context, "Arial", 12, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 0, 0, 0, 1); // 6
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
CGContextSetTextMatrix(context, CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0));
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(45));
CGContextShowTextAtPoint(context,10, 10, "Menu", 4);
CGAffineTransformMakeRotation expects angle in radians, not in degrees.
#define DEGREES_TO_RADIANS(x) (M_PI * (x) / 180.0)
...
CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(45));
[view setTransform:CGAffineTransformMakeRotation(M_PI / 2)];