gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:41:22 27 4
gpt4 key购买 nike

我有一个我无法解决的奇怪问题。如果我用我的应用程序拍照并对其应用 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% 并且它崩溃了,但是如果我从我的相机胶卷中选择图像,我只会得到最大值。 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 类型可选。你永远不知道它是否会被处理。最好检查一下。

关于ios - 如果我将 CIFilter 应用于 CIImage - Swift 2.0,应用程序会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31902478/

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