Place Image on larger canvas size using GPU (possi

2019-07-07 02:11发布

问题:

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

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

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

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...


Original Image 100x100

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.

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);

回答1:

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.

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