Swift NSMallocException:从CGDataProvider加载时无法增长缓冲区

Swift NSMallocException:从CGDataProvider加载时无法增长缓冲区,swift,Swift,我正在使用此代码模糊图像: extension CGImage { func blurEffect(_ boxSize: CGFloat) -> CGImage? { let boxSize = boxSize - (boxSize.truncatingRemainder(dividingBy: 2)) + 1 let inProvider = self.dataProvider let height = vImagePixel

我正在使用此代码模糊图像:

extension CGImage {
    func blurEffect(_ boxSize: CGFloat) -> CGImage? {

        let boxSize = boxSize - (boxSize.truncatingRemainder(dividingBy: 2)) + 1

        let inProvider = self.dataProvider

        let height = vImagePixelCount(self.height)
        let width = vImagePixelCount(self.width)
        let rowBytes = self.bytesPerRow

        let inBitmapData = inProvider?.data // Crash reported for this line
        let inData = UnsafeMutableRawPointer(mutating: CFDataGetBytePtr(inBitmapData))
        var inBuffer = vImage_Buffer(data: inData, height: height, width: width, rowBytes: rowBytes)

        let outData = malloc(self.bytesPerRow * self.height)
        var outBuffer = vImage_Buffer(data: outData, height: height, width: width, rowBytes: rowBytes)

        vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, nil, 0, 0, UInt32(boxSize), UInt32(boxSize), nil, vImage_Flags(kvImageEdgeExtend))

        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let context = CGContext(data: outBuffer.data, width: Int(outBuffer.width), height: Int(outBuffer.height), bitsPerComponent: 8, bytesPerRow: outBuffer.rowBytes, space: colorSpace, bitmapInfo: self.bitmapInfo.rawValue)
        let imageRef = context?.makeImage()

        free(outData)

        return imageRef
    }
}
有时,有报告称在第
行let inBitmapData=inProvider?上发生崩溃。data

Fatal Exception: NSMallocException: Failed to grow buffer
0  CoreFoundation                 0x1cbcf4ec4 __exceptionPreprocess
1  libobjc.A.dylib                0x1caec5a40 objc_exception_throw
2  Foundation                     0x1cc78a4dc _NSInitializePlatform
3  CoreFoundation                 0x1cbc28cb8 __CFReallocationFailed
4  CoreFoundation                 0x1cbc28c58 __CFSafelyReallocate
5  CoreFoundation                 0x1cbc48e10 __CFDataGrow
6  CoreFoundation                 0x1cbc48b54 CFDataSetLength
7  CoreGraphics                   0x1cd9d2934 CGDataProviderCopyData
我的理解是,图像数据太大,无法一次性加载到内存中。如果这是正确的,我如何更改代码以处理较大的图像