iOS CIFilters 到底有什么作用?

标签 ios iphone swift filter cifilter

我目前正在尝试使用由不同 CIFilter 过滤的图片填充 Collection View 。 我使用苹果的方式来获取一组过滤器。

let filterNames = CIFilter.filterNamesInCategories([kCICategoryStillImage,kCICategoryBuiltIn])

我认为这应该给我所有适用于 iPhone 上的静态图像的滤镜。 但这有点行不通。

这是我从上述方法中获得的(一些)过滤器:

["CIAccordionFoldTransition", "CIAdditionCompositing", "CIAffineClamp", "CIAffineTile", "CIAffineTransform", "CIAreaAverage", "CIAreaHistogram", "CIAreaMaximum", "CIAreaMaximumAlpha", "CIAreaMinimum", "CIAreaMinimumAlpha", "CIAztecCodeGenerator", "CIBarsSwipeTransition", "CIBlendWithAlphaMask", "CIBlendWithMask", "CIBloom", "CIBoxBlur", "CIBumpDistortion", "CIBumpDistortionLinear", "CICheckerboardGenerator", "CICircleSplashDistortion", "CICircularScreen", "CICircularWrap", "CICMYKHalftone", 

还有更多。

我用这种方法应用过滤器:

func applyFilter(image: UIImage, filterName: String) -> UIImage {

    let beginImage = CIImage(CGImage: image.CGImage!)

    let filter = CIFilter(name: filterName)!

    filter.setValue(beginImage, forKey: kCIInputImageKey)

    filter.setDefaults()

    let context = CIContext(options: nil)
    let imageRef = context.createCGImage(filter.outputImage!, fromRect: beginImage.extent)

    let newImage = UIImage(CGImage: imageRef)
    return newImage
}

前两个过滤器不起作用,因为生成的图像为零,然后一些工作,然后我得到:

[<CIAztecCodeGenerator 0x7fb89c775460> setValue:forUndefinedKey:]: this     class is not key value coding-compliant for the key inputImage.'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010cd5de65     __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010efebdeb    objc_exception_throw + 48
2   CoreFoundation                      0x000000010cd5daa9 -   [NSException raise] + 9
3   CoreImage                           0x000000010d33eea2 -[CIFilter setValue:forUndefinedKey:] + 137
4   CoreImage                           0x000000010d4093ce -[CIAztecCodeGenerator setValue:forUndefinedKey:] + 335
5   Foundation                          0x000000010d6749bb -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288
6   DrawIt                              0x000000010cb55088 _TFC6DrawIt25applyFilterViewController11applyFilterfS0_FTCSo7UIImage10filterNameSS_S1_ + 552
7   DrawIt                              0x000000010cb54b52 _TFC6DrawIt25applyFilterViewController14collectionViewfS0_FTCSo16UICollectionView22cellForItemAtIndexPathCSo11NSIndexPath_CSo20UICollectionViewCell + 1186
8   DrawIt                              0x000000010cb54e3f _TToFC6DrawIt25applyFilterViewController14collectionViewfS0_FTCSo16UICollectionView22cellForItemAtIndexPathCSo11NSIndexPath_CSo20UICollectionViewCell + 79
9   UIKit                               0x000000010e31d5ba -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:] + 483
10  UIKit                               0x000000010e31fae0 -[UICollectionView _updateVisibleCellsNow:] + 4431
11  UIKit                               0x000000010e32423b -[UICollectionView layoutSubviews] + 247
12  UIKit                               0x000000010db7f4a3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
13  QuartzCore                          0x0000000113d0059a -[CALayer layoutSublayers] + 146
14  QuartzCore                          0x0000000113cf4e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
15  QuartzCore                          0x0000000113cf4cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
16  QuartzCore                          0x0000000113ce9475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
17  QuartzCore                          0x0000000113d16c0a _ZN2CA11Transaction6commitEv + 486
18  QuartzCore                          0x0000000113d259f4 _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 576
19  CoreFoundation                      0x000000010ccbdc84 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
20  CoreFoundation                      0x000000010ccbd831 __CFRunLoopDoTimer + 1089
21  CoreFoundation                      0x000000010cc7f241 __CFRunLoopRun + 1937
22  CoreFoundation                      0x000000010cc7e828 CFRunLoopRunSpecific + 488
23  GraphicsServices                    0x0000000113bc8ad2 GSEventRunModal + 161
24  UIKit                               0x000000010dac8610 UIApplicationMain + 171
25  DrawIt                              0x000000010cb569fd main + 109
26  libdyld.dylib                       0x000000010faf492d start + 1
27  ???                                 0x0000000000000001 0x0 + 1
)
 libc++abi.dylib: terminating with uncaught exception of type NSException

我没有检查所有其余的过滤器。 我认为数组应该只包含易于应用的过滤器。 是代码错误还是我使用的过滤器数组错误?

但是例如第一个过滤器(CIAccordionFoldTransition)似乎是一个过渡,而不是真正的过滤器。因此它的输出为零是有道理的。

有没有办法像这样获取适用于单张图片的所有过滤器?

我对 iO 还很陌生,希望我的问题对于这个主页来说不是太愚蠢,请友善! 提前致以问候和感谢。

最佳答案

CIFilter.filterNamesInCategories 返回的某些过滤器具有 inputImage 属性,而有些则没有。

例如,CIAztecCodeGenerator(在异常报告中提到)理解的属性列于 here ,并且 inputImage 不是其中之一。

过滤器通过其 inputKeys property 报告其输入属性的键。它通过 attributes property 描述了它的所有属性。 .

也许您希望将过滤器限制为具有 inputImage 属性的过滤器:

let filterNames = CIFilter.filterNamesInCategories([kCICategoryStillImage,kCICategoryBuiltIn])
    .filter { CIFilter(name: $0)?.inputKeys.contains("inputImage") ?? false }

关于iOS CIFilters 到底有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34477337/

相关文章:

iphone - 将 View Controller 类拆分为 VC 和自定义类的规则是什么?

swift - 封装类是否反射(reflect)了 Swift 3 范例?

iphone - 在 iPhone 上使用 ASIHTTPRequest 登录用 Ruby on Rails 编写的 Web 服务并使用 AuthLogic

swift - 无法将类型 'SCNMatrix4' 的值转换为预期的参数类型 'matrix_float4x4'(又名 'simd_float4x4')

Swift - Visionkit 如何编辑按钮 "Keep Scan"和 "Retake"的颜色

iphone - 为客户、我们的开发帐户构建应用程序还是为他们设置一个?

ios - 从另一个 View Controller 更新 UIPageControl

ios - 尝试将数据传递到容器 View ,而 segue 不起作用

iOS block 和同步性

iphone - NSString stringWithUTF8String 内存泄漏