计算一个UIImage保存到相册

发布于 2024-08-06 05:18:23 字数 933 浏览 6 评论 0原文

我基本上想从一堆源图像自动创建平铺图像,然后将其保存到用户的相册中。我没有成功地将一堆小 UIImage 绘制成一个大 UIImage。实现这一目标的最佳方法是什么?目前我正在使用 UIGraphicsBeginImageContext() 和 [UIImage drawAtPoint] 等。我最终得到的只是一个 512x512 的黑色正方形。我应该怎么做?我正在寻找 CGLayer 等,似乎有很多选择,但没有一个特别容易工作。

让我实际将我的代码放入:

CGSize size = CGSizeMake(512, 512);
UIGraphicsBeginImageContext(size);
UIGraphicsPushContext(UIGraphicsGetCurrentContext());
for (int i = 0; i < 4; i++)
{
    for (int j = 0; j < 4; j++)
    {
        UIImage *image = [self getImageAt:i:j];
        [image drawAtPoint:CGPointMake(i*128,j*128)];
    }
}
UIGraphicsPopContext();
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(result, nil, nil, nil);

我应该注意,实际上上面的内容完全是我的代码中发生的事情。真正发生的情况是,我调用 UIGraphicsPushContext 之前的每一行(包括 UIGraphicsPushContext),然后在动画计时器中我慢慢增加绘图并绘制到上下文。然后,在这一切完成后,我从 UIGraphicsPopContext 开始调用所有内容。

I basically want to automatically create a tiled image from a bunch of source images, and then save that to the user's photo album. I'm not having any success drawing a bunch of small UIImage's into one big UIImage. What's the best way to accomplish this? Currently I'm using UIGraphicsBeginImageContext() and [UIImage drawAtPoint], etc. All I ever end up with is a 512x512 black square. How should I be doing this? I'm looking to CGLayer's, etc. seems there are a lot of options but none that work particularly easily.

Let me actually put my code in:

CGSize size = CGSizeMake(512, 512);
UIGraphicsBeginImageContext(size);
UIGraphicsPushContext(UIGraphicsGetCurrentContext());
for (int i = 0; i < 4; i++)
{
    for (int j = 0; j < 4; j++)
    {
        UIImage *image = [self getImageAt:i:j];
        [image drawAtPoint:CGPointMake(i*128,j*128)];
    }
}
UIGraphicsPopContext();
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(result, nil, nil, nil);

I should note that actually the above is not what happens in my code exactly. What really happens is that I call every line before and including to UIGraphicsPushContext, then in an animation timer I slowly increment the drawing and draw to the context. Then after it's all done I call everything from UIGraphicsPopContext onward.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£噩梦荏苒 2024-08-13 05:18:23

哦,那么您可以在屏幕上渲染后保存屏幕视图:

 UIGraphicsBeginImageContext(myBigView.bounds.size);
    [view drawRect:myBigView.bounds];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

您是否将其存储回图像?

UIImage *myBigImage = UIGraphicsGetImageFromCurrentImageContext();

Oh, then you can just save the onscreen view after it has been rendered on screen:

 UIGraphicsBeginImageContext(myBigView.bounds.size);
    [view drawRect:myBigView.bounds];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

are you storing it back to an image?

UIImage *myBigImage = UIGraphicsGetImageFromCurrentImageContext();
肩上的翅膀 2024-08-13 05:18:23

为了准确地完成我想做的事情...

使您的 GLView 与您想要的总大小一样大。还要确保 glOrtho 和您的视口具有正确的尺寸。然后就可以在任何地方绘制任何您想要的内容,并拍摄单个 OpenGL 屏幕截图。然后,您无需担心在多个 OpenGL 渲染通道上组合为单个 UIImage,这无疑是导致我的问题的原因。

To do exactly what I wanted to do...

Make your GLView as big as the total thing you want. Also make sure glOrtho and your viewport have the right size. Then just draw whatever you want wherever you want, and take a single OpenGL screenshot. Then you don't need to worry about combining to a single UIImage over multiple OpenGL rendering passes, which is no doubt what was causing my issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文