setting UIImageView content mode after applying a CIFIlter(应用 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 确实拉伸图像 - 以适应.如果您根本不想拉伸图像,请使用 Center(例如).
(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,从而为您提供一个实际上有一些位(CGImage,位图)的 UIImage.我在这里的讨论为您提供了演示代码:
(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.aeth.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 绘制到图形上下文中.例如,您可以将其绘制到图像图形上下文中,然后使用 that 图像.
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.
你不能做的是直接显示来自 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 内容模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:应用 CIFIlter 后设置 UIImageView 内容模式
基础教程推荐
- NSString intValue 不能用于检索电话号码 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
