通过电子邮件发送时 iPhone 图像分辨率提高

发布于 2024-10-14 18:39:03 字数 2385 浏览 3 评论 0原文

在我的应用程序中,我捕获图像,然后向其添加框架...... 我还具有在最终图像(原始图像+框架)上添加自定义文本的功能。我正在使用以下代码来绘制文本。

    -(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];
}

我使用 UIImageJPEGRepresentation 通过电子邮件发送图像并附加数据。

当我通过电子邮件发送图像而不添加自定义文本时,图像大小从 1050 x 1275 增加到 2100 x 2550,这很奇怪。 但是,当我通过电子邮件发送添加了文本的图像时,图像大小保持不变。 谁能解释一下为什么会发生这种情况? 我认为从 UIImage 转换为 UIData 与此有关。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只有影子陪我不离不弃 2024-10-21 18:39:03

我也有同样的问题。通过以 1 的比例启动上下文来修复此问题:

UIGraphicsBeginImageContextWithOptions(size, YES, 1.0);

I had the same problem. Fix it with starting the context with a scale of 1:

UIGraphicsBeginImageContextWithOptions(size, YES, 1.0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文