【问题标题】:Putting text over an image programmatically in Objective-C在 Objective-C 中以编程方式将文本放在图像上
【发布时间】:2015-10-14 23:32:51
【问题描述】:

我想以编程方式在图像上添加文本,但我似乎找不到如何做到这一点。我在这里找到了one 解决方案,但是很多东西都被弃用了,所以它不起作用......

请帮忙!

编辑:

这是我的代码:

UIImage *backgroundImage = image;

NSMutableDictionary *stringAttributes = [NSMutableDictionary dictionary];

[stringAttributes setObject: [UIFont fontWithName:@"Helvetica" size:20] forKey: NSFontAttributeName];
[stringAttributes setObject: [UIColor whiteColor] forKey: NSForegroundColorAttributeName];
[stringAttributes setObject: [NSNumber numberWithFloat: 2.0] forKey: NSStrokeWidthAttributeName];
[stringAttributes setObject: [UIColor blackColor] forKey: NSStrokeColorAttributeName];

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

NSString *myString = [NSString stringWithFormat:@"Yolo"];

[myString drawInRect:CGRectMake(0, 0, 200, 50) withAttributes:stringAttributes];

UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.imageView.image = result;

新编辑:

我想澄清一些事情以更好地理解这个问题。我的应用程序允许用户通过短信或电子邮件发送他们自己拍摄的照片,我想在照片上添加一些预先编写的字符串文本。

所以我的问题是:如何从字符串中获取文本到照片上?

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    花了很长时间,但我想通了。

    代码:

    NSMutableDictionary *stringAttributes = [NSMutableDictionary dictionary];
    
    [stringAttributes setObject: [UIFont fontWithName:@"Avenir Book" size:250] forKey: NSFontAttributeName];
    [stringAttributes setObject: [UIColor whiteColor] forKey: NSForegroundColorAttributeName];
    
    NSString *placeString = [NSString stringWithFormat:@"Text here."];
    
    CGSize size = [placeString sizeWithAttributes:stringAttributes];
    //Create a bitmap context into which the text will be rendered.
    UIGraphicsBeginImageContext(size);
    //Render the text.
    [placeString drawAtPoint:CGPointMake(0.0, 0.0) withAttributes:stringAttributes];
    //Retrieve the image.
    UIImage *imagene = UIGraphicsGetImageFromCurrentImageContext();
    
    UIImage *mergedImage = _imageView.image;
    
    CGSize newSize = image.size;
    UIGraphicsBeginImageContext(newSize);
    
    //Use existing opacity as is.
    [mergedImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    
    //Apply supplied opacity if applicable.
    CGRect drawingRect = (CGRect) {.size = size};
    drawingRect.origin.x = (newSize.width - size.width) * 0.01;
    drawingRect.origin.y = (newSize.height - size.height) * 0.03;
    [imagene drawInRect:drawingRect blendMode:kCGBlendModeNormal alpha:1];
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    [_imageView setImage:newImage];
    self.imageView.image = newImage;
    

    【讨论】:

      【解决方案2】:

      这是一种通用方法,可能缺少代码,但它为您提供了重要的部分。

       CGContextRef ctx = CGBitmapContextCreate(...)
       CGContextDrawImage (gtx, myContextRect, myimage);
      
       CGContextSelectFont(ctx, "Helvetica", 10.0, kCGEncodingMacRoman);
       CGContextSetCharacterSpacing(ctx, 1.7);
       CGContextSetTextDrawingMode(ctx, kCGTextFill);
       CGContextShowTextAtPoint(ctx, 100.0, 100.0, "SOME TEXT", 9);
      
       CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
       UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
      

      这将为您提供可以放入视图中的图像,或通过 UIActivityViewController 共享。我相信你可以解决这些问题。 方法是: 1) 创建位图上下文。 2) 渲染图像 3) 添加文字 4) 将上下文保存到图像

      【讨论】:

        【解决方案3】:

        只需在 IB 中的 UIImageView 顶部放置一个 UILabel 并使用约束根据需要定位它们。然后根据需要将文本设置到标签中。

        编辑:

        既然我知道您希望能够保存,我建议您使用 UIGraphicsBeginImageContextWithOptions 创建一个屏幕外图形上下文。我发现这比 CGContext 和 Core Graphics 调用更容易处理。

        绘制到上下文中并从中获取和图像。

        将上下文设置为图像的大小,并传入 0 的比例以使用设备的原生比例。

        您的代码可能如下所示:

        //Create an off-screen graphics context for drawing.
        CGSize imageSize = myImage.size;
        UIGraphicsBeginImageContextWithOptions(imageSize, false, 0);
        
        //draw your image using drawAtPoint(CGPointmake(0,0));
        //draw your string using one of the drawAtPoint or drawInRect methods
        //available in NSString UIKit additions
        
        //Fetch the resulting image from the context.
        UIImage *maskImage = UIGraphicsGetImageFromCurrentImageContext();
        
        //End the off-screen context
        UIGraphicsEndImageContext();
        

        【讨论】:

        • 我想以编程方式进行,因为用户将能够通过文本消息发送图像,并且文本必须刻录到图像上:/
        • 通过短信发送图片是什么意思?@simonsayzhi
        • 好的,你澄清了你想要的我的回答不适用。没关系。但是为什么有人拒绝投票给我?我的回答是基于问题中提供的信息。
        • @DuncanC 我已将代码添加到问题中。打不开,可以看看吗?
        • 我编辑了我的答案,以展示如何创建一个在图像顶部添加文本的新图像。你试过了吗?
        猜你喜欢
        • 1970-01-01
        • 2014-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多