【问题标题】:write text on image view image在图像视图图像上写文字
【发布时间】:2012-11-14 10:27:40
【问题描述】:

目前,我正在开发一个应用程序,其中我有一个图像视图,图像不断

一段时间后会发生变化。现在我希望我能够写一些文字或绘制任何符号

,simple line,cross line 表示在图像上绘图出现在图像视图中。表示我想

在图像上添加文本或绘制一些符号或线条等。我到处搜索,但不能

找到任何解决方案。我该如何解决这个问题?提前致谢。

【问题讨论】:

  • 你不能在 UIImageView 上放置一个 UILabel 吗?

标签: iphone uiimageview uiimage ios6


【解决方案1】:

你有两个选择。

  1. 添加UILabel作为imageView的子视图,并设置UILabel的backgroundcolor为clear color。
  2. 实现如下方法,以图片和文字为参数,返回文字添加的图片。

    //给UIImage添加文字

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

参考:http://iphonesdksnippets.com/post/2009/05/05/Add-text-to-image-%28UIImage%29.aspx

【讨论】:

    【解决方案2】:

    看看这是否有效:

    //Add text to UIImage
    
    -(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];
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 2011-08-20
      • 2012-07-04
      • 2020-12-06
      • 1970-01-01
      • 2017-09-15
      相关资源
      最近更新 更多