ios – 从UIImage获取CIImage(Swift)

尝试从UI Image获取CI Image以便将其用于CIFilter,但在最后一行获得以下异常:
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,100,100))
CGContextSetFillColorWithColor(con,UIColor.blueColor().CGColor)
CGContextFillPath(con)
let im:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

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

更新:
根据不将CIImage实例化为变量的建议,我重新设计了我的初始代码片段,以便在游乐场中工作:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,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)

解决方法

如果您正在尝试使用过滤器,我解决了这个操场错误:
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())

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...