gpt4 book ai didi

ios - 用于在图像上叠加文本和图形的图形库和函数

转载 作者:行者123 更新时间:2023-11-28 22:50:40 25 4
gpt4 key购买 nike

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

因此,我的用户将启动我的应用程序,然后该应用程序将使用 UIImagePickerController 访问相机并通过委托(delegate)获取图像数据。

然后我想在屏幕上显示它,然后显示控件以添加可见图像(或具体 Logo ),并让用户选择添加一两行文本,然后将这些文本放置在屏幕的某个区域图片。

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

那么,我需要知道的是,SDK 中可用的众多库中有哪些可以让我执行此操作? CG图像?考虑到我的叠加 Logo 将是透明的 PNG,我如何将一个叠加在另一个上?

我听说您可以使用 CALayer 进行呈现,但我不知道您如何将所有层抓在一起(压平它们)。

在 PHP 中,您会使用像 imagecopymerge() 这样的东西,我在计算等价物时遇到了麻烦。

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

谢谢

最佳答案

好吧,要在图像上添加文本,我相信您可以使用文本字段获取输入,然后将文本放入标签中。这部分可以通过几种不同的方式完成,但相当简单。

就抓取所有图层的组合图像而言,您可以使用 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、电子邮件等共享。拥有它会增加您的应用程序的值(value)。我建议实现它。

关于ios - 用于在图像上叠加文本和图形的图形库和函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12077106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com