【问题标题】:App crashes if I apply a CIFilter to a CIImage - Swift 2.0如果我将 CIFilter 应用于 CIImage - Swift 2.0,应用程序崩溃
【发布时间】:2015-08-11 21:02:45
【问题描述】:

我有一个我无法解决的奇怪问题。如果我用我的应用程序拍照并对其应用 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% 并且它会崩溃,但如果我从相机胶卷中选择图像,我只会得到一个最大值。 RAM 使用量的 8%。

如果你知道原因,如果你能留下答案,我会很高兴。

【问题讨论】:

    标签: ios swift crash ram core-image


    【解决方案1】:

    由于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 类型设为可选。你永远不知道它是否会被处理。最好检查一下。

    【讨论】:

    • 一个问题。如果我这样做 imageData = UIImagePNGRepresentation(processImage(selectedImage!, colors: 2)) imageData 为零。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      • 2015-03-01
      • 2020-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多