In my app i capture an image and then add a frame to it... I also have the feature to add custom text on the final image (original image + frame). I am using the following code to draw the text.
    -(UIImage *)addText:(UIImage *)img text:(NSString *)textInput
{
    CGFloat imageWidth = img.size.width;
    CGFloat imageHeigth = img.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, imageWidth, imageHeigth, 8, 
                                                 4 * imageWidth, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeigth), img.CGImage);
    CGContextSetCMYKFillColor(context, 0.0, 0.0, 0.0, 1.0,1);
    CGContextSetFont(context, customFont);
    UIColor * strokeColor = [UIColor blackColor];
    CGContextSetFillColorWithColor(context, strokeColor.CGColor); 
    CGContextSetFontSize(context, DISPLAY_FONT_SIZE * DisplayToOutputScale);
    // Create an array of Glyph's the size of text that will be drawn.
    CGGlyph textToPrint[[textInput length]];
    for (int i = 0; i < [textInput length]; ++i) 
    { 
        // Store each letter in a Glyph and subtract the MagicNumber to get appropriate value.
        textToPrint[i] = [textInput  characterAtIndex:i] + 3 - 32;
    }
    // First pass to be displayed invisible, will be used to calculate the length of the text in glyph
    CGContextSetTextDrawingMode(context, kCGTextInvisible);
    CGContextShowGlyphsAtPoint(context, 0 , 0 , textToPrint, [textInput length]);
    CGPoint endPoint = CGContextGetTextPosition(context);   
    // Calculate position of text on white border frame
    CGFloat xPos = (imageWidth/2.0f) - (endPoint.x/2.0f);
    CGFloat yPos; 
    yPos = 30 * DisplayToOutputScale;
    // Toggle off invisible mode, we are ready to draw the text
    CGContextSetTextDrawingMode开发者_如何学编程(context, kCGTextFill); 
    CGContextShowGlyphsAtPoint(context, xPos , yPos , textToPrint, [textInput length]);
    // Extract resulting image
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    return [UIImage imageWithCGImage:imageMasked];
}
I email the image using UIImageJPEGRepresentation and attach the data.
When i email the image without adding custom text the image size increases from 1050 x 1275 to 2100 x 2550 which is strange. But when i email the image with text added the image size remains unchanged. Can any one explain me why this happens ?? I think there is something to do with converting from UIImage to UIData.
Thanx
I had the same problem. Fix it with starting the context with a scale of 1:
UIGraphicsBeginImageContextWithOptions(size, YES, 1.0);
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论