【问题标题】:Value of type 'UnsafeMutableRawPointer' has no subscripts Swift 5'UnsafeMutableRawPointer' 类型的值没有下标 Swift 5
【发布时间】:2020-01-05 11:00:17
【问题描述】:

我正在尝试将目标 c 代码转换为 swift。

UInt8* data = calloc(bytesPerRow, height);

当我快速转换它时,它返回“UnsafeMutableRawPointer”。我需要 UInt8。

let data = calloc(bytesPerRow, height)! // UnsafeMutableRawPointer

我尝试了不同的解决方案,但对我不起作用。谁能告诉我如何解决这个问题。谢谢

“UnsafeMutableRawPointer”类型的值没有下标

Swift 代码。

 // data pointer – stores an array of the pixel components. For example (r0, b0, g0, a0, r1, g1, b1, a1 .... rn, gn, bn, an)
        //*calloc(size_t __count, size_t __size) __result_use_check __alloc_size(1,2);
        let data = calloc(bytesPerRow, height)!

        // get new RGB color space
        let colorSpace = CGColorSpaceCreateDeviceRGB()

        // create bitmap context
        let ctx = CGContext(data: data, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)

        // draw image into context (populating the data array while doing so)
        ctx?.draw(rawImage!, in: rect)

        // get the index of the pixel (4 components times the x position plus the y position times the row width)
        var pixelIndex = 4 * (pixelPosition.x + (pixelPosition.y * width))

        // set the pixel components to the color components
        data[pixelIndex] = color255Components[0]; // r
        data[pixelIndex+1] = color255Components[1]; // g
        data[pixelIndex+2] = color255Components[2]; // b
        data[pixelIndex+3] = color255Components[3]; // a

目标 C 代码。

// data pointer – stores an array of the pixel components. For example (r0, b0, g0, a0, r1, g1, b1, a1 .... rn, gn, bn, an)
    UInt8* data = calloc(bytesPerRow, height);

    // get new RGB color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // create bitmap context
    CGContextRef ctx = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo);

    // draw image into context (populating the data array while doing so)
    CGContextDrawImage(ctx, rect, rawImage);

    // get the index of the pixel (4 components times the x position plus the y position times the row width)
    NSInteger pixelIndex = 4*(pixelPosition.x+(pixelPosition.y*width));

    // set the pixel components to the color components
    data[pixelIndex] = color255Components[0]; // r
    data[pixelIndex+1] = color255Components[1]; // g
    data[pixelIndex+2] = color255Components[2]; // b
    data[pixelIndex+3] = color255Components[3]; // a

【问题讨论】:

  • @MANIAK_dobrii,我已经看过了。 “UnsafeMutablePointer.allocate(capacity: Int)”中只有一个参数。如果你看到目标 c 代码 "*calloc(size_t __count, size_t __size)" 它有两个参数
  • @ZAFAR007:这个答案也适用于你的情况。 calloc() 接受两个简单相乘的参数。因此UnsafeMutablePointer<UInt8>.allocate(capacity: bytesPerRow * height) 可以解决问题。
  • 感谢@Martin R 和 MANIAK_dobrii。我不知道相乘,它也有效。
  • 感谢@Rob,是的,我正在使用这个“免费(数据)”来释放它。

标签: ios objective-c swift uint8t unsafemutablepointer


【解决方案1】:

您需要为可变指针分配一个类型。在当前状态下,它是一个指向任何东西的可变指针,但您希望它是一个指向 UInt8 值缓冲区的可变指针。

为此,只需在您的数据上调用assumingMemoryBound

let data = calloc(bytesPerRow, height)!.assumingMemoryBound(to: UInt8.self)

【讨论】:

  • 谢谢,它有效,你节省了我的时间。我试图从几个小时内解决这个问题。
  • @ZAFAR007 很高兴听到这个消息。在这种情况下,我有点不确定是否应该调用 bindMemory 而不是 assumingMemoryBound。这些事情永远都不够清楚。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多