2015-12-27 48 views
2

我目前正試圖用不同的CIFilter過濾的圖片填充集合視圖。 我用蘋果的方式得到一個過濾器的數組。iOS CIFilters實際上有用嗎?

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

我認爲這應該給我適用於iPhone上的靜止圖像的所有過濾器。 但它有點不起作用。

這是過濾器i。從上述方法得到的(一些):

["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 

我沒有檢查所有其他的過濾器。 我認爲該數組應該只包含適用於此的易用的flters。 它是錯誤的代碼或我使用的過濾器數組?

但例如第一個過濾器(CIAccordionFoldTransition)似乎是一個過渡不是過濾器。所以它是有道理的,它沒有輸出。

有沒有辦法讓它們適用於單畫面,就像所有的過濾器?

我在iOs中很新鮮,希望我的問題是不要爲這個主頁愚蠢,要善良! 問候並提前致謝。

回答

5

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 } 
+0

非常感謝,將嘗試!例如手風琴摺疊有一個inputimage,但我想它沒有輸出(是零),因爲它是一個過渡,有沒有辦法檢查?因爲鑰匙在那裏,它只是不會返回任何東西。也許我應該使用一些捕獲工作過濾器編譯我自己的數組? – Marcel

+0

燁非常感謝您的解決方案保持了一個錯誤,但我似乎無法找到任何列表或解釋其過濾器做什麼......我只能嘗試出來.. – Marcel

+0

[核心圖像過濾器參考(https://開頭developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/uid/TP40004346)(的iOS版)。 –