Add Text on UIImage

风流意气都作罢 提交于 2019-12-21 19:24:34

问题


Hi I am using this function to draw text on Image after the drawing on image is done but when I do this its showing me transparent text i.e. Text with opacity which I do not want it should be with alpha 1.0 I tried to add the fontcolor attribute with RGBA but dint work

-(UIImage*) drawText:(NSString*) text
             inImage:(UIImage*)  image
             atPoint:(CGPoint)   point
             atSize :(CGSize) size
{
    //[NSLog(@"Drawing Text on the Image : Text Posn x:%f  y:%f ",point.x,point.y);
    UIGraphicsBeginImageContext(_mainViewForDrawing.frame.size);

    [image drawInRect:CGRectMake(_mainViewForDrawing.frame
                                 .origin.x,_mainViewForDrawing.frame.origin.y,_mainViewForDrawing.frame.size.width,_mainViewForDrawing.frame.size.height)];
    CGPoint pt;
    pt.x = point.x + _camera2EditText.frame.size.width/2;
    pt.y = point.y + _camera2EditText.frame.size.height/2;

    UITextPosition *post = [_camera2EditText beginningOfDocument];


    CGRect r = [_camera2EditText caretRectForPosition:post];

    //[NSLog(@"Here is the rect: %f %f ",r.origin.x,r.origin.y);

    CGRect rect = CGRectMake(r.origin.x, point.y + r.origin.y, _mainViewForDrawing.frame.size.width,_mainViewForDrawing.frame.size.height);
    [[UIColor whiteColor] set];

    //UIFont *font = [UIFont boldSystemFontOfSize:TextFontSize];

    UIFont *font = [UIFont fontWithName:TEXT_FONT_NAME size:TEXT_FONT_SIZE];

    if([text respondsToSelector:@selector(drawInRect:withAttributes:)])
    {
        //iOS 7
       // NSDictionary *att = @{NSFontAttributeName:font};
        NSDictionary *att = @{ NSFontAttributeName: font, NSForegroundColorAttributeName: [UIColor whiteColor]};
        [text drawInRect:rect withAttributes:att];
    }
    else
    {
        //legacy support
        [text drawInRect:CGRectIntegral(rect) withFont:font];
    }

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

    return newImage;
}

How to go about it


回答1:


This is my method I use for drawing text onto an image, hopefully this will help. It uses the an attributed string instead to set the properties that you want:

+(UIImage*)drawFront:(UIImage*)image text:(NSString*)text atPoint:(CGPoint)point
{
    UIFont *font = [UIFont fontWithName:@"Halter" size:21];
    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
    CGRect rect = CGRectMake(point.x, (point.y - 5), image.size.width, image.size.height);
    [[UIColor whiteColor] set];

    NSMutableAttributedString* attString = [[NSMutableAttributedString alloc] initWithString:text];
    NSRange range = NSMakeRange(0, [attString length]);

    [attString addAttribute:NSFontAttributeName value:font range:range];
    [attString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:range];

    NSShadow* shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor darkGrayColor];
    shadow.shadowOffset = CGSizeMake(1.0f, 1.5f);
    [attString addAttribute:NSShadowAttributeName value:shadow range:range];

    [attString drawInRect:CGRectIntegral(rect)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

Should be easy enough for you to adapt to your needs... Let me know if you struggle!




回答2:


If you want to add the text on the image and want to take the input from the user, then you can place that textview inside the UIView and make the image and merge with the background.

//Draw the text view to create a image from it

        UIGraphicsBeginImageContextWithOptions(viewInputText.bounds.size, true, 0)
        viewInputText.drawViewHierarchyInRect(viewInputText.bounds, afterScreenUpdates: true)
        let imageWithText = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        //create a UIImage from the current context and save it to camera roll.
        UIGraphicsBeginImageContextWithOptions(imgCorrection.bounds.size, true, 0)

        imgCorrection.image?.drawInRect(CGRect(x: 0, y: 0,
            width: imgCorrection.frame.size.width, height: imgCorrection.frame.size.height))

        //Draw the text image in the current context to make a single image.
        imageWithText.drawInRect(viewInputText.frame)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        //save image on the camera roll.

For more details please refer the following link



来源:https://stackoverflow.com/questions/25302799/add-text-on-uiimage

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