简体   繁体   English

如何在图像iOS上写文字?

[英]How to write a text on image iOS?

In my app I'm trying to add a text on an image. 在我的应用中,我试图在图像上添加文本。 I've done it using a post in stackoverflow. 我已经在stackoverflow中使用了一个帖子。 It's like the following. 就像下面这样。

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

When I'm writing a text using this code, the original image's colour is changed like the following, 当我使用此代码编写文本时,原始图像的颜色将如下更改,

Original Image 原始图片

在此处输入图片说明

Output with Text 带文字输出

在此处输入图片说明

Can anyone explain why is this happening. 谁能解释为什么会这样。 and how to fix this?? 以及如何解决这个问题?

Thanks in Advance!! 提前致谢!!

We need to create a image context and apply drawing and get final image from this context. 我们需要创建一个图像上下文并应用绘图并从该上下文中获取最终图像。 You can try this method. 您可以尝试此方法。 Hope this help you. 希望这对您有所帮助。

+ (UIImage *) addText:(UIImage *)img text:(NSString *)text
{
    CGRect rect = [[UIScreen mainScreen] bounds];

    // create a context according to image size
    UIGraphicsBeginImageContext(rect.size);

    // draw image
    [img drawInRect:rect];


    UIFont* font = [UIFont systemFontOfSize:14.0];

    /// Make a copy of the default paragraph style
    NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    /// Set line break mode
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    /// Set text alignment
    paragraphStyle.alignment = NSTextAlignmentCenter;

    NSDictionary *attributes = @{ NSFontAttributeName: font,
                                  NSParagraphStyleAttributeName: paragraphStyle };

    CGRect textRect = CGRectMake(20, 160.0, 280.0, 44);

    /// draw text
    [text drawInRect:textRect withAttributes:attributes];

    // get as image
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

I am using this and it works fine for me 我正在使用它,对我来说很好

- (UIImage*) drawText:(NSString*) text
         inImage:(UIImage*)  image
         atPoint:(CGPoint)   point
{

    UIFont *font = [UIFont fontWithName:FONT_BOLD size:20];

    UIGraphicsBeginImageContext(image.size);

    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];

    CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);

    [UIColorFromRGB(0x515151) set];

    [text drawInRect:CGRectIntegral(rect) withFont:font];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;
}

EDIT: This can only be run on the main thread! 编辑:这只能在主线程上运行! So it's pretty much useless. 因此,这几乎没有用。

+(UIImage *)imageFromView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

Here's a new one using Core Graphics that can be used on any thread 这是使用Core Graphics的新版本,可以在任何线程上使用

+ (UIImage *)imageFromView:(UIView *)view
{
    size_t width = view.bounds.size.width*2;
    size_t height = view.bounds.size.height*2;

    unsigned char *imageBuffer = (unsigned char *)malloc(width*height*8);
    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef imageContext = CGBitmapContextCreate(imageBuffer, width, height, 8, width*4, colourSpace,kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big);

    CGColorSpaceRelease(colourSpace);

    CGContextTranslateCTM(imageContext, 0.0, height);
    CGContextScaleCTM(imageContext, 2.0, -2.0);

    [view.layer renderInContext:imageContext];

    CGImageRef outputImage = CGBitmapContextCreateImage(imageContext);
    UIImage *image = [[UIImage alloc] initWithCGImage:outputImage];

    CGImageRelease(outputImage);
    CGContextRelease(imageContext);
    free(imageBuffer);

    return image;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM