如果我将 CIFilter 应用于 CIImage,应用程序会崩溃 - Swift 2.0

App crashes if I apply a CIFilter to a CIImage - Swift 2.0

我有一个我无法解决的奇怪问题。如果我用我的应用程序拍照并对其应用 CIFilter,一切正常,但如果我拍摄另一张照片并应用相同的过滤器,我的应用程序就会崩溃而不会出现任何错误。但奇怪的是,我可以对我的相机胶卷中的图像执行相同的过程,而且应用程序运行得非常好。

//CAMERA

@IBOutlet weak var imageView: UIImageView!

var selectedImage: UIImage!    

@IBAction func rollButtonPressed(sender: UIBarButtonItem) {
    let picker = UIImagePickerController()

    picker.delegate = self
    picker.sourceType = .PhotoLibrary

    self.presentViewController(picker, animated: true, completion: nil)
}

@IBAction func cameraButtonPressed(sender: UIBarButtonItem) {
    if UIImagePickerController.isSourceTypeAvailable(.Camera){

        let camera = UIImagePickerController()
        camera.delegate = self
        camera.allowsEditing = false
        camera.sourceType = UIImagePickerControllerSourceType.Camera
        camera.showsCameraControls = true

        camera.cameraOverlayView?.layer.borderWidth = 3
        camera.cameraOverlayView?.layer.borderColor = UIColor.redColor().CGColor
        camera.cameraOverlayView?.frame = CGRectMake(self.view.frame.minX, self.view.frame.midY - self.view.frame.width/8, self.view.frame.width, self.view.frame.width/4)


        self.presentViewController(camera, animated: true, completion: nil)
    }

}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    self.dismissViewControllerAnimated(true, completion: nil)

    selectedImage = (info[UIImagePickerControllerOriginalImage] as! UIImage)

    for var i = 2; i <= 10; i++ {
        imageView.image = processImage(selectedImage, colors: i)
    }

}

导致问题的函数:

let context = CIContext(options: nil)

func processImage(image: UIImage, colors: Int) -> UIImage{

    var img1:CIImage = CIImage(image: image)!
    img1 = img1.imageByApplyingFilter("CIPhotoEffectMono", withInputParameters:["inputImage" : img1])
    img1 = img1.imageByApplyingFilter("CISharpenLuminance", withInputParameters: ["inputImage" : img1])
    img1 = img1.imageByApplyingFilter("CIColorPosterize", withInputParameters: ["inputImage" : img1, "inputLevels" : colors])
    img1 = img1.imageByApplyingFilter("CIColorControls", withInputParameters: ["inputImage" : img1, "inputSaturation" : 1, "inputBrightness": 0.2, "inputContrast": 1.5 //8
        ])
    img1 = img1.imageByApplyingFilter("CIHighlightShadowAdjust", withInputParameters: ["inputImage" : img1, "inputHighlightAmount": 6, "inputShadowAmount": 0])
    img1 = img1.imageByApplyingFilter("CIColorPosterize", withInputParameters: ["inputImage" : img1, "inputLevels" : 3])


    let cgImg = context.createCGImage(img1, fromRect: img1.extent)

    let uiImg = UIImage(CGImage: cgImg, scale: 1.0, orientation: image.imageOrientation)
    print("x")
    return uiImg
}

我查看了 RAM 使用情况,如果我使用来自相机的图像,我的应用程序使用了大约 40% 并且它崩溃了,但是如果我 select 来自我的相机胶卷的图像,我只得到最大。 8% 的 RAM 使用率。

如果你知道为什么,如果你能留下答案,我会很高兴。

由于 createCGImage.

,您的内存不足

为我工作processImage 职能:

func processImage(image: UIImage, colors: Int) -> UIImage{
    var img1:CIImage = CIImage(image: image)!
    img1 = img1.imageByApplyingFilter("CIPhotoEffectMono", withInputParameters:["inputImage" : img1])
    img1 = img1.imageByApplyingFilter("CISharpenLuminance", withInputParameters: ["inputImage" : img1])
    img1 = img1.imageByApplyingFilter("CIColorPosterize", withInputParameters: ["inputImage" : img1, "inputLevels" : colors])
    img1 = img1.imageByApplyingFilter("CIColorControls", withInputParameters: ["inputImage" : img1, "inputSaturation" : 1, "inputBrightness": 0.2, "inputContrast": 1.5 //8
        ])
    img1 = img1.imageByApplyingFilter("CIHighlightShadowAdjust", withInputParameters: ["inputImage" : img1, "inputHighlightAmount": 6, "inputShadowAmount": 0])
    img1 = img1.imageByApplyingFilter("CIColorPosterize", withInputParameters: ["inputImage" : img1, "inputLevels" : 3])

    let uiImg = UIImage(CIImage: img1)
    print("x")
    return uiImg ?? UIImage()
}

而且我会将返回的 UIImage 类型设为可选。你永远不知道它是否会被处理。最好检查一下。

我不得不在 processImage 的开头添加 image = UIImage(data: UIImageJPEGRepresentation(image, 0.9)!)!,因为我的图像太大了。