点击一个button将文字放在图像上

所以我想从某些文本字段添加文本到图像时,我调用一个IBAction,以便在图像的某些地方的字段中创build一个新的图像。 我甚至不知道从哪里开始,所以我不能提供任何代码我有,对不起。 任何帮助表示赞赏。

正如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中的图像replace为图像,或者用新图像做任何你想做的事情。

您也可以尝试这种方式,而不是添加一个标签到视图,重新绘制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); } }