2014-11-23 105 views
8

試圖從UIImage的,以將其用於CIFilter獲得CIImage,但收到以下異常在最後一行:獲取CIImage從UIImage的(SWIFT)

Execution was interrupted, reason: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0) 

我在做什麼錯?

import UIKit 

UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,100), false, 0) 
let con:CGContextRef = UIGraphicsGetCurrentContext() 
CGContextAddEllipseInRect(con, CGRectMake(0,0,100,100)) 
CGContextSetFillColorWithColor(con, UIColor.blueColor().CGColor) 
CGContextFillPath(con) 
let im:UIImage = UIGraphicsGetImageFromCurrentImageContext() 
UIGraphicsEndImageContext() 

let ciimage = CIImage(image: im) // <- Exception here 

UPDATE: 繼建議不要實例CIImage作爲可變我返工我最初的代碼片段在操場的工作:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,100), false, 0) 
let con:CGContextRef = UIGraphicsGetCurrentContext() 
CGContextAddEllipseInRect(con, CGRectMake(0,0,100,100)) 
CGContextSetFillColorWithColor(con, UIColor.blueColor().CGColor) 
CGContextFillPath(con) 
let im:UIImage = UIGraphicsGetImageFromCurrentImageContext() 
UIGraphicsEndImageContext() 

//let ciimage = CIImage(image: im) // <- this was causing an exception 

let filter = CIFilter(name: "CIGaussianBlur", withInputParameters: [kCIInputRadiusKey: 10, kCIInputImageKey: CIImage(image: im)]) // <- this does not cause an exception 
let calayer = CALayer() 
calayer.contents = CIContext(options:nil).createCGImage(filter.outputImage, fromRect: filter.outputImage.extent()) 
calayer.frame = CGRect(x: 0, y: 0, width: 270, height: 270) 
var view = UIView() 
view.frame = calayer.frame 
view.layer.addSublayer(calayer) 
XCPShowView("view", view) 
+1

這失敗了在操場上,但在編譯代碼爲我工作 - 必須是一個怪癖操場環境。 – 2014-11-23 04:08:21

+0

是的,我也爲我編譯。 – 2014-11-23 04:33:43

回答

8

如果你試圖使用過濾器來工作,我解決了這個遊樂場的bug:

let pic = UIImage(named: "crumpled.jpg") 
let filter = CIFilter(name: "CISepiaTone") 
filter.setValue(CIImage(image: pic), forKey: kCIInputImageKey) 
filter.setValue(0.8, forKey: kCIInputIntensityKey) 
let ctx = CIContext(options:nil) 
let cgImage = ctx.createCGImage(filter.outputImage, fromRect:filter.outputImage.extent()) 
+0

謝謝,工作! – Paul 2014-12-30 23:51:08