如何在iOS图像上写文字?

时间:2014-10-21 05:42:52

标签: ios objective-c uiimage graphic

在我的应用中,我尝试在图片上添加文字。我已经使用stackoverflow中的帖子完成了它。它就像以下一样。

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

当我使用此代码编写文本时,原始图像的颜色会发生如下变化,

原始图片

enter image description here

以文字输出

enter image description here

任何人都可以解释为什么会发生这种情况。以及如何解决这个问题?

先谢谢!!

3 个答案:

答案 0 :(得分:2)

我们需要创建一个图像上下文并应用绘图并从此上下文中获取最终图像。你可以尝试这种方法。希望这能帮助你。

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

答案 1 :(得分:1)

我正在使用它,它对我来说很好用

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

答案 2 :(得分:1)

编辑:这只能在主线程上运行!所以它几乎没用。

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

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

    return image;
}

这是一个使用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;
}