如何在iphone中的图像上添加文字? [英] How can I add a text on an image in iphone?

查看:121
本文介绍了如何在iphone中的图像上添加文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在iPhone上的图像上放置文字。它就像Eurosport iPhone应用程序。

I need to put a text over an image in iPhone. It's like the Eurosport iPhone app.

http://a2.mzstatic.com/us/r1000/092/Purple/v4/fa/ d6 / 9e / fad69e44-29e2-a123-2a74-ad58ba5de36e / mza_1083026897138509692.320x480-75.jpg

以同样的方式我需要放一个文字在我的应用程序中我怎么能这样做?

In the same way I need to put a text in my app. How can I do this?

谢谢。

推荐答案

我找到了两种方式

1:

    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(0, 25, 200, 200);
    imageView.image = [UIImage imageNamed:@"Choice set.png"];

    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
    myLabel.text = @"Hello There";
    myLabel.textColor = [UIColor whiteColor];
    myLabel.backgroundColor = [UIColor blueColor];
    [imageView addSubview:myLabel];

    [self.view addSubview:imageView];

    [imageView release];
    [myLabel release];

2:

向UIImage添加文字

-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{

   int w = img.size.width;
   int h = img.size.height;
   CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
   CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);

   char* text= (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
   CGContextSelectFont(context, "Arial",20, kCGEncodingMacRoman);
   CGContextSetTextDrawingMode(context, kCGTextFill);
   CGContextSetRGBFillColor(context, 0, 0, 0, 1);
   CGContextShowTextAtPoint(context,10,10,text, strlen(text));
   CGImageRef imgCombined = CGBitmapContextCreateImage(context);

   CGContextRelease(context);
   CGColorSpaceRelease(colorSpace);

   UIImage *retImage = [UIImage imageWithCGImage:imgCombined];
   CGImageRelease(imgCombined);

   return retImage;
} 

然后致电

UIImage *image = [UIImage imageNamed:@"myimage.png"];

imageView.image = [self addText:image text:@"Hello there"];

这篇关于如何在iphone中的图像上添加文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆