【问题标题】:Render Text on Image, Save Image in full resolution?在图像上渲染文本,以全分辨率保存图像?
【发布时间】:2013-06-26 13:06:39
【问题描述】:

我需要为图像添加一个文本标签,然后让用户能够保存带有文本的图像。问题是,我找不到任何关于如何做到这一点的好信息。迹象表明使用 UIKit 的 UIGraphicsBeginImageContextUIGraphicsGetImageFromCurrentImageContextUIGraphicsEndImageContext。我什至不确定这是否是正确的查看方向。我需要在保存图像时不要缩小图像。他们必须保持他们的决心。

我很茫然。关于在哪里寻找或下一步尝试什么的任何建议或帮助都会非常有帮助。

【问题讨论】:

    标签: ios objective-c uiimage uikit


    【解决方案1】:

    这是一个从视图(包括它的子视图)创建UIImage 的方法。假设图像在UIImageView 中,将用户文本添加到UILabel 中作为UIImageView 的子视图。

    +(UIImage *)imageFromView:(UIView *)view{
    
        // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
        // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
    
        CGSize imageSize = view.bounds.size;
        if (NULL != UIGraphicsBeginImageContextWithOptions)
            UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
        else
            UIGraphicsBeginImageContext(imageSize);
    
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        [view.layer renderInContext:context];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
        return image;
    }
    

    目前它使用视图的大小作为图像的大小。如果您希望它具有不同的大小,则可以传入所需的结果图像大小并根据需要设置imageSize

    您需要将 QuartzCore 基础添加到您的项目中并添加 #import <QuartzCore/QuartzCore.h> 到您的 .m 文件中。

    这是UIViewController 中的适用代码,用于将生成的图像调整为源图像的大小,其中大部分在saveButtonTapped 方法中:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        sourceImage = [UIImage imageNamed:@"animal"];
        self.onScreenImageView.image = sourceImage;
    }
    
    -(void)viewWillAppear:(BOOL)animated{
        NSLog(@"Source image size: %0.0f x %0.0f", sourceImage.size.width, sourceImage.size.height);
        NSLog(@"onScreenImageView size: %0.0f x %0.0f", self.onScreenImageView.frame.size.width, self.onScreenImageView.frame.size.height);
    }
    
    - (IBAction)saveButtonTapped:(id)sender {
    
        UIImageView *offScreenImageView = [[UIImageView alloc] initWithImage:sourceImage];
        NSLog(@"offScreenImageView size: %0.0f x %0.0f", offScreenImageView.frame.size.width, offScreenImageView.frame.size.height);
    
        UILabel *offScreenLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 1000, 100)];
        offScreenLabel.font = [UIFont systemFontOfSize:80.f];
        offScreenLabel.textColor = [UIColor blackColor];
        offScreenLabel.text = @"User Image Label Text";
        [offScreenImageView addSubview:offScreenLabel];
    
        UIImage *labeledImage = [NAHLabelPictureVC imageFromView:offScreenImageView];
        NSLog(@"Labeled image size: %0.0f x %0.0f", labeledImage.size.width, labeledImage.size.height);
        self.labeledImageView.image = labeledImage;
    }
    

    以及NSLogs 的输出:

    2013-06-28 15:57:13.052 SOSandbox[28772:907] Source image size: 1213 x 1159
    2013-06-28 15:57:13.055 SOSandbox[28772:907] onScreenImageView size: 200 x 200
    2013-06-28 15:57:14.963 SOSandbox[28772:907] offScreenImageView size: 1213 x 1159
    2013-06-28 15:57:14.992 SOSandbox[28772:907] Labeled image size: 1213 x 1159
    

    【讨论】:

    • 在我问这个问题之前,我之前在 Apple 的文档中阅读过这个。如何创建与从相机捕获的图像的实际大小相同的图像?这让我感到困惑,因为这最终会在手机上创建一些非常小的东西,不是吗?
    • 或者我误解了这个方法如何处理数据/图像捕获?
    • 您传入的视图不需要在设备的屏幕上,因此您可以创建例如UIImageView,其大小与源图像相同。然后将您的UILabel 添加为子视图。在用户指示应保存标记的图像后执行此“屏幕外”操作。
    • 根据需要,可能需要增加UILabel字体的大小,并根据原图大小计算其边框。
    • 嗯。你知道我可以在哪里找到示例代码吗?我找了几个小时,在这个话题上找不到太多东西。
    猜你喜欢
    • 1970-01-01
    • 2021-09-19
    • 2014-05-25
    • 2015-10-20
    • 1970-01-01
    • 2012-04-23
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多