单击按钮将文本放在图像上

时间:2016-01-25 18:31:12

标签: ios objective-c

所以我想在调用IBAction时将某些文本字段中的文本添加到图像中,以便使用图像某些位置上的字段中的文本创建新图像。我甚至不知道从哪里开始,所以我无法提供任何代码,抱歉。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

正如MSU_Budog已经提到的,你只需在imageView上添加一个标签。然后将imageView传递给此方法:

- (UIImage *)getScreenShotFromView:(UIView*)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

并保存图像或将imageView中的图像替换为图像,或使用新图像执行任何操作。

答案 1 :(得分:0)

你也可以尝试这种方式,而不是在视图中添加标签,用图像和图像上的文字重新绘制uiview。后来拍摄视图的快照:

- (void)drawTextOnViewWithFrame: (CGRect)frame
{
    //// General Declarations
    CGContextRef context = UIGraphicsGetCurrentContext();


    //// Image Declarations
    UIImage * img = [UIImage imageNamed: @"your-image.png"];

    //// Rectangle Drawing
    UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(0, 0, 510, 888)];
CGContextSaveGState(context);
[rectanglePath addClip];
[img drawInRect: CGRectMake(0, 0, img.size.width, img.size.height)];
CGContextRestoreGState(context);



    //// Text Drawing
CGRect textRect = CGRectMake(x,y,w,h);
{
    NSString* textContent = @"Hello, World!"; //Text to draw
    NSMutableParagraphStyle* textStyle = NSMutableParagraphStyle.defaultParagraphStyle.mutableCopy;
    textStyle.alignment = NSTextAlignmentLeft;

    NSDictionary* textFontAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize: UIFont.labelFontSize], NSForegroundColorAttributeName: UIColor.blackColor, NSParagraphStyleAttributeName: textStyle};

    CGFloat textTextHeight = [textContent boundingRectWithSize: CGSizeMake(textRect.size.width, INFINITY)  options: NSStringDrawingUsesLineFragmentOrigin attributes: textFontAttributes context: nil].size.height;
    CGContextSaveGState(context);
    CGContextClipToRect(context, textRect);
    [textContent drawInRect: CGRectMake(CGRectGetMinX(textRect), CGRectGetMinY(textRect) + (CGRectGetHeight(textRect) - textTextHeight) / 2, CGRectGetWidth(textRect), textTextHeight) withAttributes: textFontAttributes];
    CGContextRestoreGState(context);
}

}