更新UIImageView的UIImage时EXC_BAD_ACCESS(代码= 1)

时间:2019-05-02 23:23:09

标签: ios swift coreml

我是iOS和CoreML的新手。我有一个非常简单的UI,带有两个UIImageViews(一个应该是输入,第二个应该是输出)。点击第一个时,图像应由神经网络处理,输出应显示在第二个中。

但是,当我尝试从MLMultiArray输出对象下载图像并从中创建UIImage时,可以将其上传到第二个UIImageView时,我得到EXC_BAD_ACCESS(code = 1)。

我已经减少了根本不调用神经网络处理的问题,只是尝试从MLMultiArray创建新图像。结果是一样的。

之后,我尝试从一个空的缓冲区生成一个UIImage。图像创建正确,但是如果我尝试更新UIImageView以使用它,则会遇到相同的错误。

如果我尝试将第二个UIImageView更新为其他图像(例如输入图像),一切正常。

我认为这是关于我正在创建的UIImage对象的内存管理问题,但我无法弄清楚自己在做什么错

class ViewController: UIViewController {

    @IBOutlet weak var out: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func imageTapped(_ sender: UITapGestureRecognizer) {
        let imageView = sender.view as? UIImageView

        if let imageToAnalyse = imageView?.image {
            if let outImg = process(forImage: imageToAnalyse) {
                out.image = outImg
            }
        }
    }

    func process (forImage inImage:UIImage) -> UIImage? {

        let size = CGSize(width: 512, height: 512)
        let mlOut = try? MLMultiArray(shape: [1, size.height, size.width] as [NSNumber], dataType: .float32)
        let newImage = getSinglePlaneImage(inBuffer: mlOut!, width: Int(size.width), height: Int(size.height))

        return newImage
    }

    func getSinglePlaneImage(inBuffer: MLMultiArray, width: Int, height: Int) -> UIImage
    {
        var newImage: UIImage

//        let floatPtr =  inBuffer.dataPointer.bindMemory(to: Float32.self, capacity: inBuffer.count)
//        let floatBuffer = UnsafeBufferPointer(start: floatPtr, count: inBuffer.count)
//        let pixelValues : [UInt8]? = Array(floatBuffer).map({UInt8( ImageProcessor.clamp( (($0) + 1.0) * 128.0, minValue: 0.0, maxValue: 255.0) ) })

        //simulating pixels from MLMultiArray 
        let pixels : [UInt8]? = Array(repeating: 0, count: 512*512)

        var imageRef: CGImage?

        if var pixelValues = pixels {
            let bitsPerComponent = 8
            let bytesPerPixel = 1
            let bitsPerPixel = bytesPerPixel * bitsPerComponent
            let bytesPerRow = bytesPerPixel * width
            let totalBytes = height * bytesPerRow

            imageRef = withUnsafePointer(to: &pixelValues, {
                ptr -> CGImage? in
                var imageRef: CGImage?
                let colorSpaceRef = CGColorSpaceCreateDeviceGray()
                let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue).union(CGBitmapInfo())
                let data = UnsafeRawPointer(ptr.pointee).assumingMemoryBound(to: UInt8.self)
                let releaseData: CGDataProviderReleaseDataCallback = {
                    (info: UnsafeMutableRawPointer?, data: UnsafeRawPointer, size: Int) -> () in
                }

                if let providerRef = CGDataProvider(dataInfo: nil, data: data, size: totalBytes, releaseData: releaseData) {
                    imageRef = CGImage(width: width,
                                       height: height,
                                       bitsPerComponent: bitsPerComponent,
                                       bitsPerPixel: bitsPerPixel,
                                       bytesPerRow: bytesPerRow,
                                       space: colorSpaceRef,
                                       bitmapInfo: bitmapInfo,
                                       provider: providerRef,
                                       decode: nil,
                                       shouldInterpolate: false,
                                       intent: CGColorRenderingIntent.defaultIntent)
                }


                return imageRef
            })
        }

        newImage = UIImage(cgImage: imageRef!)

        return newImage
    }
}

1 个答案:

答案 0 :(得分:1)

似乎您的代码将512x512-float32数组成功转换为512x512-UInt8数组,因此我根据您的代码的未注释版本编写此答案。 (尽管,转换效率不高,还有一些改进的余地。)

更新

以下描述不是解决OP问题的正确解决方案。仅作记录。请跳至此答案底部的更新代码

旧代码(不是正确的解决方案)

首先,代码中最严重的缺陷是以下两行:

imageRef = withUnsafePointer(to: &pixelValues, {

    let data = UnsafeRawPointer(ptr.pointee).assumingMemoryBound(to: UInt8.self)

上面的第一行将指针指向[UInt8]?,在Swift中,[UInt8]?(又名Optional<Array<UInt8>>)是一个8字节的结构,而不是像C数组那样的连续区域。 / p>

第二个更危险。 ptr.pointee[UInt8]?,但不能保证通过指针访问Swift数组。并且将Array传递给UnsafeRawPointer.init(_:)可能会创建一个临时区域,该区域会在调用初始化程序后立即释放。

如您所知,偶尔在有限的条件下访问悬空指针不会造成危害,但随时可能会产生意外结果。


我会这样写:

func getSinglePlaneImage(inBuffer: MLMultiArray, width: Int, height: Int) -> UIImage {

    //simulating pixels from MLMultiArray
    //...
    let pixelValues: [UInt8] = Array(repeating: 0, count: 1*512*512)

    let bitsPerComponent = 8
    let bytesPerPixel = 1
    let bitsPerPixel = bytesPerPixel * 8
    let bytesPerRow = bytesPerPixel * width
    let totalBytes = height * bytesPerRow

    let imageRef = pixelValues.withUnsafeBytes({bytes -> CGImage? in
        var imageRef: CGImage?
        let colorSpaceRef = CGColorSpaceCreateDeviceGray()
        let bitmapInfo: CGBitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue)

        let data = bytes.baseAddress!.assumingMemoryBound(to: UInt8.self)
        let releaseData: CGDataProviderReleaseDataCallback = {_,_,_ in }

        if let providerRef = CGDataProvider(dataInfo: nil, data: data, size: totalBytes, releaseData: releaseData) {
            imageRef = CGImage(width: width,
                               height: height,
                               bitsPerComponent: bitsPerComponent,
                               bitsPerPixel: bitsPerPixel,
                               bytesPerRow: bytesPerRow,
                               space: colorSpaceRef,
                               bitmapInfo: bitmapInfo,
                               provider: providerRef,
                               decode: nil,
                               shouldInterpolate: false,
                               intent: .defaultIntent)
        }


        return imageRef
    })

    let newImage = UIImage(cgImage: imageRef!)

    return newImage
}

当您想要一个指向数组起始元素的指针时,请使用withUnsafeBytes并在闭包参数中使用该指针(实际上,它是UnsafeRawBufferPointer)。

再有一个,您的pixelspixelValues不必是可选的。


否则,您可以为每个像素创建Float32的灰度图像。

func getSinglePlaneImage(inBuffer: MLMultiArray, width: Int, height: Int) -> UIImage {

    //simulating pixels from MLMultiArray
    //...
    let pixelValues: [Float32] = Array(repeating: 0, count: 1*512*512)

    let bitsPerComponent = 32 //<-
    let bytesPerPixel = 4 //<-
    let bitsPerPixel = bytesPerPixel * 8
    let bytesPerRow = bytesPerPixel * width
    let totalBytes = height * bytesPerRow

    let imageRef = pixelValues.withUnsafeBytes({bytes -> CGImage? in
        var imageRef: CGImage?
        let colorSpaceRef = CGColorSpaceCreateDeviceGray()
        let bitmapInfo: CGBitmapInfo = [CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue),
                          .byteOrder32Little, .floatComponents] //<-
        let data = bytes.baseAddress!.assumingMemoryBound(to: Float32.self)
        let releaseData: CGDataProviderReleaseDataCallback = {_,_,_ in }

        if let providerRef = CGDataProvider(dataInfo: nil, data: data, size: totalBytes, releaseData: releaseData) {
            imageRef = CGImage(width: width,
                               height: height,
                               bitsPerComponent: bitsPerComponent,
                               bitsPerPixel: bitsPerPixel,
                               bytesPerRow: bytesPerRow,
                               space: colorSpaceRef,
                               bitmapInfo: bitmapInfo,
                               provider: providerRef,
                               decode: nil,
                               shouldInterpolate: false,
                               intent: CGColorRenderingIntent.defaultIntent)
        }


        return imageRef
    })

    let newImage = UIImage(cgImage: imageRef!)

    return newImage
}

两项工作均符合我的测试项目中的预期,但是如果发现有问题,请通知我。


更新代码(希望这是正确的解决方案)

我错过了一个事实,即即使创建了CGDataProvider之后,init(dataInfo:data:size:releaseData:)在用CGImage创建时仍保留指针。因此,withUnsafeBytes的关闭结束后,当无效时,可以引用它。

在这种情况下,您最好使用CGDataProvider.init(data:)

func getSinglePlaneImage(inBuffer: MLMultiArray, width: Int, height: Int) -> UIImage {
    var newImage: UIImage

    //let floatPtr =  inBuffer.dataPointer.bindMemory(to: Float32.self, capacity: inBuffer.count)
    //let floatBuffer = UnsafeBufferPointer(start: floatPtr, count: inBuffer.count)
    //let pixelValues: Data = Data((floatBuffer.lazy.map{
    //    UInt8(ImageProcessor.clamp((($0) + 1.0) * 128.0, minValue: 0.0, maxValue: 255.0))
    //})

    //simulating pixels from MLMultiArray
    //...
    let pixelValues = Data(count: 1*512*512) // <- ###

    var imageRef: CGImage?

    let bitsPerComponent = 8
    let bytesPerPixel = 1
    let bitsPerPixel = bytesPerPixel * bitsPerComponent
    let bytesPerRow = bytesPerPixel * width

    let colorSpaceRef = CGColorSpaceCreateDeviceGray()
    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue)

    if let providerRef = CGDataProvider(data: pixelValues as CFData) { // <-###
        imageRef = CGImage(width: width,
                           height: height,
                           bitsPerComponent: bitsPerComponent,
                           bitsPerPixel: bitsPerPixel,
                           bytesPerRow: bytesPerRow,
                           space: colorSpaceRef,
                           bitmapInfo: bitmapInfo,
                           provider: providerRef,
                           decode: nil,
                           shouldInterpolate: false,
                           intent: CGColorRenderingIntent.defaultIntent)
    }

    newImage = UIImage(cgImage: imageRef!)

    return newImage
}

据我尝试,即使在实际设备中重复触摸的次数也不会崩溃。请试试。感谢您的耐心等候。