【问题标题】:How to combine UIImage and UILabel into one image and save如何将 UIImage 和 UILabel 合并为一张图片并保存
【发布时间】:2012-11-11 10:07:54
【问题描述】:

我有 2 个 UILabel 和 2 个图像需要合并到一个 UIImage 中保存。

我知道我可以用屏幕截图来做到这一点,但我的主图像是圆形的,所以如果我把它矫正,它仍然会显示出锐利的边缘。

我可以这样做来合并图像:

//CGSize newImageSize = CGSizeMake(cropImage.frame.size.width, cropImage.frame.size.height);
CGSize newImageSize = CGSizeMake(480, 320);
NSLog(@"CGSize %@",NSStringFromCGSize(newImageSize));

UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0); //retina res
[self.viewForImg.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

NSData *imgData =  UIImageJPEGRepresentation(image, 0.9); //UIImagePNGRepresentation ( image ); // get JPEG representation
UIImage * imagePNG = [UIImage imageWithData:imgData]; // wrap UIImage around PNG representation

UIGraphicsEndImageContext();
return imagePNG;

但不确定如何在 UILabel 中添加。

非常感谢任何回复。

【问题讨论】:

    标签: iphone ios xcode uiimageview uiimage


    【解决方案1】:
    UIEdgeInsets insets = UIEdgeInsetsMake(1, 1, 1, 1);
    CGSize imageSizeWithBorder = CGSizeMake(view.frame.size.width + insets.left + insets.right, view.frame.size.height + insets.top + insets.bottom);
    UIGraphicsBeginImageContextWithOptions(imageSizeWithBorder, UIEdgeInsetsEqualToEdgeInsets(insets, UIEdgeInsetsZero), 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClipToRect(context, (CGRect){{insets.left, insets.top}, view.frame.size});
    CGContextTranslateCTM(context, -view.frame.origin.x + insets.left, -view.frame.origin.y + insets.top);
    [view.layer renderInContext:context];
    UIImage *viewCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    试试这个!

    【讨论】:

      【解决方案2】:

      使用[myLabel.layer renderInContext:UIGraphicsGetCurrentContext()]; 在当前上下文中绘制。

      例如:-

          UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0); //retina res
          [self.viewForImg.layer renderInContext:UIGraphicsGetCurrentContext()];
          [myLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
          UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
      

      根据您的 cmets,如果您想在特定帧中绘制它,请按以下步骤操作,

      [myLabel drawTextInRect:CGRectMake(0.0f, 0.0f, 100.0f, 50.0f)];
      

      如果你想给背景上色,试试这个,

      CGRect drawRect = CGRectMake(rect.origin.x, rect.origin.y,rect.size.width, rect.size.height); 
      CGContextSetRGBFillColor(context, 100.0f/255.0f, 100.0f/255.0f, 100.0f/255.0f, 1.0f); 
      CGContextFillRect(context, drawRect);
      

      或者你可以检查这个问题Setting A CGContext Transparent Background

      【讨论】:

      • 感谢 ACB 的回复,但是这些并不能回答我的问题。我使用 [myLabel drawTextInRect:CGRectMake(14, 35, 220, 40)];
      • @Desmond,是的,这也是一个选项。我也想添加它,然后决定反对它,因为这应该足以绘制标签。如果问题是要绘制特定的矩形,那是正确的,但看不出有问题。
      • 别担心,我觉得我的问题还不够清楚。谢谢 :) 顺便问一下,您知道如何更改 UIGraphicsGetImageFromCurrentImageContext 的背景颜色吗?当我保存图像时,背景是白色的。
      • 类似CGRect drawRect = CGRectMake(rect.origin.x, rect.origin.y,rect.size.width, rect.size.height); CGContextSetRGBFillColor(context, 100.0f/255.0f, 100.0f/255.0f, 100.0f/255.0f, 1.0f); CGContextFillRect(context, drawRect); 或查看此stackoverflow.com/questions/2125543/…
      【解决方案3】:
       UIGraphicsBeginImageContextWithOptions(newImageSize, NO, scale); //retina res
              [COGI.layer renderInContext:UIGraphicsGetCurrentContext()];
              [COGI.image drawInRect:CGRectMake(0, 0, 248, 290)];
              [iconI.image drawInRect:CGRectMake(4, 20, 240, 240)];
              [stampI.image drawInRect:CGRectMake(0, -5, 248, 290)];
              [headerL drawTextInRect:CGRectMake(14, 35, 220, 40)];
              [detailL drawTextInRect:CGRectMake(16, 200, 215, 65)];
      
              UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
              [[UIColor redColor] set]; 
              NSData *imgData =  UIImageJPEGRepresentation(image, 1.0); //UIImagePNGRepresentation ( image ); // get JPEG representation
              UIImage * imagePNG = [UIImage imageWithData:imgData]; // wrap UIImage around PNG representation
      
              UIGraphicsEndImageContext();
              return imagePNG;
      

      【讨论】:

        猜你喜欢
        • 2022-01-19
        • 1970-01-01
        • 1970-01-01
        • 2016-03-31
        • 2011-11-04
        • 1970-01-01
        • 2017-09-18
        相关资源
        最近更新 更多