ios – 从UIImage获取CIImage(Swift)

前端之家收集整理的这篇文章主要介绍了ios – 从UIImage获取CIImage(Swift)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
尝试从UI Image获取CI Image以便将其用于CIFilter,但在最后一行获得以下异常:
  1. Execution was interrupted,reason: EXC_BREAKPOINT (code=EXC_I386_BPT,subcode=0x0)

我究竟做错了什么?

  1. import UIKit
  2.  
  3. UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,100),false,0)
  4. let con:CGContextRef = UIGraphicsGetCurrentContext()
  5. CGContextAddEllipseInRect(con,CGRectMake(0,100,100))
  6. CGContextSetFillColorWithColor(con,UIColor.blueColor().CGColor)
  7. CGContextFillPath(con)
  8. let im:UIImage = UIGraphicsGetImageFromCurrentImageContext()
  9. UIGraphicsEndImageContext()
  10.  
  11. let ciimage = CIImage(image: im) // <- Exception here

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

  1. UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,UIColor.blueColor().CGColor)
  2. CGContextFillPath(con)
  3. let im:UIImage = UIGraphicsGetImageFromCurrentImageContext()
  4. UIGraphicsEndImageContext()
  5.  
  6. //let ciimage = CIImage(image: im) // <- this was causing an exception
  7.  
  8. let filter = CIFilter(name: "CIGaussianBlur",withInputParameters: [kCIInputRadiusKey: 10,kCIInputImageKey: CIImage(image: im)]) // <- this does not cause an exception
  9. let calayer = CALayer()
  10. calayer.contents = CIContext(options:nil).createCGImage(filter.outputImage,fromRect: filter.outputImage.extent())
  11. calayer.frame = CGRect(x: 0,y: 0,width: 270,height: 270)
  12. var view = UIView()
  13. view.frame = calayer.frame
  14. view.layer.addSublayer(calayer)
  15. XCPShowView("view",view)

解决方法

如果您正在尝试使用过滤器,我解决了这个操场错误
  1. let pic = UIImage(named: "crumpled.jpg")
  2. let filter = CIFilter(name: "CISepiaTone")
  3. filter.setValue(CIImage(image: pic),forKey: kCIInputImageKey)
  4. filter.setValue(0.8,forKey: kCIInputIntensityKey)
  5. let ctx = CIContext(options:nil)
  6. let cgImage = ctx.createCGImage(filter.outputImage,fromRect:filter.outputImage.extent())

猜你在找的iOS相关文章