将swift 2.3转换为swift 3的问题

时间:2016-11-04 15:22:16

标签: swift2 swift3

这里是将图像像素从一种颜色更改为另一种颜色的代码

 class func processPixelsInImage(inputImage: UIImage,defaultColor:RGBA32,filledColor:RGBA32,currentValue:CGFloat,maxValue:CGFloat) -> UIImage? {

        guard let context = CGBitmapContextCreate(nil, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo) else {
            print("unable to create context")
            return nil
        }

        CGContextDrawImage(context, CGRectMake(0, 0, CGFloat(width), CGFloat(height)), inputCGImage)

        let pixelBuffer = UnsafeMutablePointer<RGBA32>(CGBitmapContextGetData(context))

        var currentPixel = pixelBuffer
        let prevPixel = pixelBuffer

        for i in 0 ..< Int(CGFloat(width)/maxValue*currentValue) {
            for j in 0 ..< Int(height) {
                if currentPixel.memory.color != 0 {
                    if currentValue == maxValue {
                        currentPixel.memory = defaultColor
                    }else {
                        currentPixel.memory = filledColor
                    }
                }
                currentPixel = prevPixel
                currentPixel += (i+width*j)
            }

        }

        let outputCGImage = CGBitmapContextCreateImage(context)
        let outputImage = UIImage(CGImage: outputCGImage!, scale: inputImage.scale, orientation: inputImage.imageOrientation)
        return outputImage
    }

}

当我尝试将其转换为swift 3

let pixelBuffer = UnsafeMutablePointer<RGBA32>(CGBitmapContextGetData(context))

在这一行我必须将UnsafeMutablePointer改为

let pixelBuffer = UnsafeRawPointer(context.data)

因为在currentPixel对象的内存属性中找不到 如何获得像素记忆?

1 个答案:

答案 0 :(得分:2)

memory属性已替换为pointee属性。

来源:https://swift.org/migration-guide/