Ios 在UIImage上添加文本

Ios 在UIImage上添加文本,ios,objective-c,uiimage,uigraphicscontext,Ios,Objective C,Uiimage,Uigraphicscontext,您好,在图像绘制完成后,我正在使用此功能在图像上绘制文本,但当我这样做时,它会显示透明文本,即带有不透明度的文本,我不希望它使用alpha 1.0,我尝试使用RGBA添加fontcolor属性,但效果不佳 -(UIImage*) drawText:(NSString*) text inImage:(UIImage*) image atPoint:(CGPoint) point atSize :(CGSize) s

您好,在图像绘制完成后,我正在使用此功能在图像上绘制文本,但当我这样做时,它会显示透明文本,即带有不透明度的文本,我不希望它使用alpha 1.0,我尝试使用RGBA添加fontcolor属性,但效果不佳

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

这是我用来在图像上绘制文本的方法,希望这会有所帮助。它使用属性化字符串来设置所需的属性:

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

应该很容易适应你的需要。。。如果你挣扎,请告诉我

如果要在图像上添加文本并从用户处获取输入,则可以将该文本视图放置在UIView中,生成图像并与背景合并

//绘制文本视图以从中创建图像

        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.

有关更多详细信息,请参阅以下

以获得上述代码中生成图像的更高质量:UIGraphicsBeginImageContextWithOptions(image.size,NO,[[UIScreen mainScreen]scale]);顺便说一句,如果在该方法中发生崩溃,请首先检查字体名称。^^关于这一点,请注意,根据文档,如果将比例设置为0.0,则默认为设备主屏幕的比例因子,因此您可以将[[UIScreen mainScreen]scale]替换为0.0