write text on image view image

半城伤御伤魂 提交于 2019-12-07 17:07:04

问题


Currently, I am developing an App in which I have a Image view on which image are constantly

changes after some time. Now I want that I am able to write some text or draw any symbol

,simple line,cross line means to say drawing on image appears in image view. Means I want to

add text or drawing some symbols or lines e.t.c on image. I am searching everywhere but can't

find any solution. How can I solve this? Thanks in advance.


回答1:


See if this works :

//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];

}



回答2:


You have two options.

  1. Add a UILabel as the subview of imageView and set the backgroundcolor of UILabel to clear color.
  2. Implement the following method, which takes image and text as argument and returns the text added image.

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

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



来源:https://stackoverflow.com/questions/13377034/write-text-on-image-view-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!