线程1:EXC_BAD_ACCESS(代码= EXC_I386_GPFLT)

时间:2016-09-29 04:58:58

标签: ios swift xcode

在我的swift项目中,我有两个类一起工作来保存图像的Pixel值,以便能够修改红色,绿色,蓝色和alpha值。 UnsafeMutableBufferPointer包含许多由Pixel类对象组成的咬合。

我可以与拥有UnsafeMutableBufferPointer<Pixel>属性的类进行交互。我可以访问该对象的所有属性,一切正常。我使用UnsafeMutableBufferPoint<Pixel>时遇到的唯一问题是尝试使用我的Pixel对象遍历它,并且它一直与线程1:EXC_BAD_ACCESS (code=EXC_I386_GPFLT)异常一起崩溃。

init!(image: UIImage)
{
    _width = Int(image.size.width)
    _height = Int(image.size.height)

    guard let cgImage = image.cgImage else { return nil }

    _width = Int(image.size.width)
    _height = Int(image.size.height)
    let bitsPerComponent = 8

    let bytesPerPixel = 4
    let bytesPerRow = _width * bytesPerPixel
    let imageData = UnsafeMutablePointer<Pixel>.allocate(capacity: _width * _height)
    let colorSpace = CGColorSpaceCreateDeviceRGB()

    var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue
    bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue
    guard let imageContext = CGContext(data: imageData, width: _width, height: _height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { return nil }
    imageContext.draw(cgImage, in: CGRect(origin: CGPoint.zero, size: image.size))

    _pixels = UnsafeMutableBufferPointer<Pixel>(start: imageData, count: _width * _height)
}

此功能是崩溃程序的部分。崩溃的确切部分是循环遍历rgba.pixels的for循环。 rgba.pixels是UnsafeMutableBufferPointer。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
    let image: UIImage = info[UIImagePickerControllerEditedImage] as! UIImage!

    let rgba = RGBA(image: image)!

    for pixel in rgba.pixels
    {
        print(pixel.red)
    }

    self.dismiss(animated: true, completion: nil);
}

这是我创建UnsafeMutableBufferPointer<Pixel>的构造函数。是否有更简单的方法可以获得RBGA值并轻松更改它们。

Pixel类是UInt32值,它被分为四个UInt 8值。

我使用错误的构造来保存这些值,如果是这样,是否有更安全或更容易使用的构造?或者我在访问Pixel值时做错了什么?

1 个答案:

答案 0 :(得分:0)

这就是我得到图像像素的方法 -

// Grab and set up variables for the original image
    let inputCGImage = inputImage.CGImage
    let inputWidth: Int = CGImageGetWidth(inputCGImage)
    let inputHeight: Int = CGImageGetHeight(inputCGImage)

    // Get the colorspace that will be used for image processing (RGB/HSV)
    let colorSpace: CGColorSpaceRef = CGColorSpaceCreateDeviceRGB()!

    // Hardcode memory variables
    let bytesPerPixel = 4 // 32 bits = 4 bytes
    let bitsPerComponent = 8 // 32 bits div. by 4 components (RGBA) = 8 bits per component
    let inputBytesPerRow = bytesPerPixel * inputWidth

    // Get a pointer pointing to an allocated array to hold all the pixel data of the image
    let inputPixels = UnsafeMutablePointer<UInt32>(calloc(inputHeight * inputWidth, sizeof(UInt32)))

    // Create a context to draw the original image in (aka put the pixel data into the above array)
    let context: CGContextRef = CGBitmapContextCreate(inputPixels, inputWidth, inputHeight, bitsPerComponent, inputBytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedLast.rawValue | CGBitmapInfo.ByteOrder32Big.rawValue)!

    CGContextDrawImage(context, CGRect(x: 0, y: 0, width: inputWidth, height: inputHeight), inputCGImage)

请记住,这不是Swift 3语法,而是您正在使用的内容,但这是基本算法。现在要获取每个像素的各个颜色值,您必须实现这些功能 -

func Mask8(x: UInt32) -> UInt32
{
    return x & 0xFF
}

func R(x: UInt32) -> UInt32
{
    return Mask8(x)
}

func G(x: UInt32) -> UInt32
{
    return Mask8(x >> 8)
}

func B(x: UInt32) -> UInt32
{
    return Mask8(x >> 16)
}

func A(x: UInt32) -> UInt32
{
    return Mask8(x >> 24)
}

要在处理RGBA值后创建全新颜色,请使用此功能 -

func RGBAMake(r: UInt32, g: UInt32, b: UInt32, a: UInt32) -> UInt32
{
    return (Mask8(r) | Mask8(g) << 8 | Mask8(b) << 16 | Mask8(a) << 24)
}

要迭代像素数组,你可以这样做 -

var currentPixel = inputPixels
for _ in 0..<height
    {
        for i in 0..<width
        {
            let color: UInt32 = currentPixel.memory
            if i < width - 1
            {
                print(NSString(format: "%3.0f", R(x: color), terminator: " "))
            }
            else
            {
                print(NSString(format: "%3.0f", R(x: color)))
            }
            currentPixel += 1
        }
    }