使用GPU(可能是CIFilter)将图像放置在较大的画布上,而不使用图像上下文 [英] Place Image on larger canvas size using GPU (possibly CIFilters) without using Image Context

查看:122
本文介绍了使用GPU(可能是CIFilter)将图像放置在较大的画布上,而不使用图像上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一张100x100的图片。我想将图片放置在500x500的较大画布上。

Let's say I have an Image that's 100x100. I want to place the image onto a larger canvas size that's 500x500.

我当前的方法是使用UIGraphics创建一个Context,然后将图像绘制到该上下文上。

My current approach is to use UIGraphics to create a Context, then draw the image onto the context.

UIGraphics.BeginImageContext(....);
ImageView.Draw (....);

这很好,但是速度不如我想要的快我在做。我注意到CIFilters非常快。 是否可以使用CIFilters或其他使用GPU而不使用CPU的方法将图像放置在较大的画布上?我已经尝试过CIFilters CICrop和CIPerspectiveTransform,但是我可以似乎没有得到我想要的结果...

That works great, but it's not as fast as I'd like it to be for what I'm doing. I noticed that CIFilters are extremely fast. Is there a way I can place an image on a larger canvas size using CIFilters, or another method that uses the GPU and not the CPU?? I've experimented with the CIFilters CICrop and CIPerspectiveTransform but I can't seem to get my desired result...



原始图片100x100


Original Image 100x100

我所需的结果图片为500x500。我只是想使用CIFilters或某些GPU操作来拍摄图像并增加其画布大小。

My Desired result image at 500x500. I simply want to take the image and increase it's canvas size using CIFilters or some GPU operation.

我尝试使用CICrop进行反向裁剪,工作。请注意,即使图像本身是100x100,我也将CIVector的大小指定为500x500,结果图像完全忽略了该多余空间,如果关闭,则将其剪切掉。这是我的代码:

I tried doing a "reverse crop" using CICrop, but that didn't work. Notice I specified the size of the CIVector to be 500x500 even though the image itself is 100x100, the result image totally ignored that extra space and cut if off. Here is my code:

        CICrop crop = new CICrop();
        crop.Image = ImageView.Image.CGImage;
        crop.Rectangle = new CIVector(0, 0, 500, 500);
        ImageView.Image = new UIImage(crop.OutputImage);
        Console.WriteLine("ImageView.Image.size = "+ ImageView.Image.Size);


推荐答案

作物过滤器无法满足您的目的-您无法使用裁切滤镜缩放输入图像的尺寸。摘自Wikipedia:裁剪是指去除图像的外部部分以改善取景,突出主题或改变长宽比。

A crop filter is not going to serve your purpose - you cannot scale the size of an input image with a crop filter. From Wikipedia: Cropping refers to the removal of the outer parts of an image to improve framing, accentuate subject matter or change aspect ratio.

您需要的是比例滤镜。要使用CoreImage实现此目的,您需要执行以下操作:

What you need is a scale filter. To achieve this with CoreImage, you need to do something like this:

CIImage *input = [CIImage imageWithCGImage: ImageView.Image.CGImage]; // input image is 100 X 100
CGAffineTransform transform = CGAffineTransformMakeScale(5.0f, 5.0f); // Scale by 5 times along both dimensions
CIImage *output = [input imageByApplyingTransform: transform];
// output image is now 500 X 500

这篇关于使用GPU(可能是CIFilter)将图像放置在较大的画布上,而不使用图像上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆