gpt4 book ai didi

ios - 从 UIImage 获取 CIImage (Swift)

转载 作者:IT王子 更新时间:2023-10-29 05:50:14 25 4
gpt4 key购买 nike

尝试从 UIImage 获取 CIImage 以便将其用于 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,0,100,100))
CGContextSetFillColorWithColor(con, UIColor.blueColor().CGColor)
CGContextFillPath(con)
let im:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

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

更新:按照不要将 CIImage 实例化为变量的建议,我重新编写了我的初始代码片段以在 Playground 上工作:

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)

最佳答案

如果您尝试使用过滤器,我解决了这个 playground 错误:

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 - 从 UIImage 获取 CIImage (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27085225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com