ios - 如何在iPhone中的图像上添加文本?

标签 ios image-processing

我需要在iPhone的图片上加上文字。就像Eurosport iPhone应用程序一样。

(来源:mzstatic.com)
以同样的方式,我需要在应用程序中添加文本。我怎样才能做到这一点?
谢谢。

最佳答案

我发现了两种方法

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"];

关于ios - 如何在iPhone中的图像上添加文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11739007/

相关文章:

ios - SVG文件定义的CGPath上的CAKeyFrameAnimation有点生涩。

ios - UICollectionView 垂直居中

python - 使用 Opencv 检测图像中矩形的中心和角度

c++ - 如何填补 OpenCV 中二进制图像中的空白?

python - 如何使用python去除图像中的椒盐噪声?

ios - Facebook 登录设置失败

ios - NSStackView subview 不调整子堆栈 View 的大小

ios - Interface Builder 无法确定 "Main.storyboard"的类型。这可能是由于缺少 SDK

c++ - 如何将 pcl::Histogram 保存为 PNG 或 JPG 文件?

image-processing - 除了标准/渐进式,第三种JPEG压缩: load by channel?