哪些图形库和函数用于覆盖图像上的文本和图形

时间:2012-08-22 16:04:02

标签: ios graphics uiimage core-graphics

我需要编写一个可以拍照的应用程序然后我希望能够预览它并覆盖图形和一些文本,然后将其保存回图像库。

因此,我的用户将启动我的应用程序,然后使用UIImagePickerController访问摄像头并通过委派获取图像数据。

然后我想在屏幕上显示它,然后呈现控件以添加可见图像(或具体的徽标),并为用户提供添加可能一行或两行文本的选项,然后将其放置在图片。

然后用户点击保存,生成的合成图像将被写入相机胶卷/照片库,然后用户可以根据需要将图像上传到twitter,facebook等(我不希望实现OAuth2的开销)这可能是别人的问题。)

所以,我需要知道的是,SDK中可用的众多库中的哪一个可以让我这样做? CGImage?鉴于我的叠加徽标将是一个透明的PNG,如何将其叠加在另一个上?

我听说你可以和CALayer一起出席,但我不知道你是怎么把所有的图层拼在一起(展平它们)。

在PHP中你会使用像imagecopymerge()这样的东西,我很难搞清楚等价物。

我不想将数据传递到网络服务器并返回,因为我知道它可以在本地完成。

由于

1 个答案:

答案 0 :(得分:2)

为了在图像上添加文字,我确信你可以使用文本字段来获取输入,然后将文本放入标签中。这部分可以通过几种不同的方式完成,但相当容易。

至于抓取所有图层的组合图像,您可以使用Quartz。

每个UIWindow(继承自UIView)和UIView均由CALayer支持。 CALayer/-renderInContext:方法允许您将图层及其子图层渲染​​到图形上下文。因此,要获取整个屏幕的快照,您可以遍历屏幕上的每个窗口并将其图层层次结构渲染到目标上下文。完成后,您可以通过UIGraphicsGetImageFromCurrentImageContext功能获取屏幕截图,如下所示。

请注意,CALayer/-renderInContext:仅捕获您的UIKit和Quartz绘图。它不捕获OpenGL ES或视频内容。

#import <QuartzCore/QuartzCore.h>

.....

- (UIImage*)screenshot 
{
    // Create a graphics context with the target size
    // 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 = [[UIScreen mainScreen] bounds].size;
    if (NULL != UIGraphicsBeginImageContextWithOptions)
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    else
        UIGraphicsBeginImageContext(imageSize);

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Iterate over every window from back to front
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) 
    {
        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
        {
            // -renderInContext: renders in the coordinate space of the layer,
            // so we must first apply the layer's geometry to the graphics context
            CGContextSaveGState(context);
            // Center the context around the window's anchor point
            CGContextTranslateCTM(context, [window center].x, [window center].y);
            // Apply the window's transform about the anchor point
            CGContextConcatCTM(context, [window transform]);
            // Offset by the portion of the bounds left of and above the anchor point
            CGContextTranslateCTM(context,
                                  -[window bounds].size.width * [[window layer] anchorPoint].x,
                                  -[window bounds].size.height * [[window layer] anchorPoint].y);

            // Render the layer hierarchy to the current context
            [[window layer] renderInContext:context];

            // Restore the context
            CGContextRestoreGState(context);
        }
    }

    // Retrieve the screenshot image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}

至于上传到Facebook和Twitter,请查看ShareKit。它是一个用于与Facebook,Twitter,Tumblr,电子邮件等共享的拖放库。拥有这个可以为您的应用增加价值。我建议实施它。