应用CIFIlter后设置UIImageView内容模式 [英] setting UIImageView content mode after applying a CIFIlter

查看:168
本文介绍了应用CIFIlter后设置UIImageView内容模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您的光临。

这是我的代码

 CIImage *result = _vignette.outputImage;
self.mainImageView.image = nil;
//self.mainImageView.contentMode = UIViewContentModeScaleAspectFit;
self.mainImageView.image = [UIImage imageWithCIImage:result];
self.mainImageView.contentMode = UIViewContentModeScaleAspectFit;

此处 _vignette 已正确设置过滤器和图像效果正确应用于图像。

in here _vignette is correctly set up filter and image effect is applying to the image correctly.

我正在使用分辨率为500x375的源图像。我的imageView几乎拥有iPhone屏幕的分辨率。所以为了避免拉伸,我正在使用AspectFit。

I'm using a source image with resolution 500x375. My imageView has almost iPhone screen's resolution. So to avoid stretching I'm using AspectFit.

但是在我将结果图像分配回我的imageView后应用效果后,它会拉伸。无论我使用哪个 UIViewContentMode 。它不起作用。它似乎总是适用 ScaleToFill ,不管我给出的过滤器。

But after applying effect when I'm assigning the result image back to my imageView it streches. No matter which UIViewContentMode I use. It doesn't work. It seems it always applies ScaleToFill regardless the filter I've given.

任何想法为什么会发生这种情况?任何建议都非常感谢。

Any idea why is this happening? Any suggestion is highly appreciated.

推荐答案

(1)Aspect Fit 拉伸图像 - 适合。如果您根本不想拉伸图像,请使用中心(例如)。

(1) Aspect Fit does stretch the image - to fit. If you don't want the image stretched at all, use Center (for example).

(2) imageWithCIImage 给你一个非常奇怪的野兽,一个不基于CGImage的UIImage,因此不受普通的图层显示规则的影响。它实际上只不过是CIImage的薄包装,这不是你想要的。你必须通过CGImage将CIFilter输出转换(渲染)到UIImage,从而为你提供一个实际上有一些位的UIImage(CGImage,一个位图)。我在这里的讨论为您提供了演示代码:

(2) imageWithCIImage gives you a very weird beast, a UIImage not based on CGImage, and so not susceptible to the normal rules of layer display. It is really nothing but a thin wrapper around CIImage, which is not what you want. You must convert (render) the CIFilter output thru CGImage to UIImage, thus giving you a UIImage that actually has some bits (CGImage, a bitmap). My discussion here gives you code that demonstrates:

http://www.apeth.com/iOSBook/ch15.html#_cifilter_and_ciimage

换句话说,在某些时候你必须调用CIContext createCGImage:fromRect:从CIFilter的输出生成CGImageRef,并将其传递给UIImage。在你这样做之前,你没有将过滤器操作的输出作为真正的UIImage。

In other words, at some point you must call CIContext createCGImage:fromRect: to generate a CGImageRef from the output of your CIFilter, and pass that on into a UIImage. Until you do that, you don't have the output of your filter operations as a real UIImage.

或者,你可以从中绘制图像。 imageWithCIImage 进入图形上下文。例如,您可以将其绘制到图像图形上下文中,然后使用那个图像。

Alternatively, you can draw the image from imageWithCIImage into a graphics context. For example, you can draw it into an image graphics context and then use that image.

不能 do直接显示来自 imageWithCIImage 的图像。那是因为它不是图像!它没有底层位图(CGImage)。那里没有。它只是一组用于导出图像的CIFilter指令。

What you can't do is display the image from imageWithCIImage directly. That's because it isn't an image! It has no underlying bitmap (CGImage). There's no there there. All it is is a set of CIFilter instructions for deriving the image.

这篇关于应用CIFIlter后设置UIImageView内容模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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