感谢您查看我的帖子。
因此,在image!.drawInRect(rect!)
行,我收到了EXC_BAD_ACCESS代码1错误,我无法解决这个问题。我已经在Run Scheme中启用了Zombies,并且没有打印出来(也许我也做错了?),我有println()看似我所有的变量,没有什么是零。
我想要注意的是,这段代码工作了两次,然后在第三次调用时失败,大部分时间都是。在我的应用程序中,你拍了一张照片,然后你需要编辑图片(当调用这个函数时)。当我回到相机拍照并返回编辑时(第3次),我收到此错误。
此外,此方法在viewDidAppear()
感谢任何帮助。我要把头发拉出来了!
var rect: CGRect?
var image: UIImage?
func convertCIImageToUIImage(cIImage: CIImage) -> UIImage {
println(cIImage)
let size: CGSize = filteredImageView.inputImage.size
println(filteredImageView.inputImage.size)
UIGraphicsBeginImageContext(size)
rect = CGRect(origin: CGPointZero, size: size)
image = UIImage(CIImage: filteredImageView.filter.outputImage)
println(UIGraphicsGetCurrentContext())
println("size: \(size)")
printAllObjects()
println()
image!.drawInRect(rect!)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
image = nil
rect = nil
let finalImage = UIImage(CGImage: newImage.CGImage!, scale: 1.0, orientation: UIImageOrientation.Right)
return finalImage!
}
答案 0 :(得分:3)
Solved it!
My function was over-engineered by a long-shot. A simple return UIImage(CIImage: cIImage)!
solved my problem and replaced all of the code above. That's what I get for copying code online! Lesson learned. Thanks for the help!
答案 1 :(得分:2)
由于有问题的行强制解开两个选项,你可能会解开一个nil(产生错误)。尝试使用可选绑定进行安全解包:
if let safeRect = CGRect(origin: CGPointZero, size: size),
let safeImage = UIImage(CIImage: filteredImageView.filter.outputImage)
{
safeImage.drawInRect(safeRect)
}
这样,如果CGRect
实例或UIImage
实例的构造失败,则不会执行大括号内的行。