'UnsafeMutableRawPointer' 类型的值没有下标 Swift 5
Value of type 'UnsafeMutableRawPointer' has no subscripts Swift 5
我正在尝试将 objective c 代码转换为 swift。
UInt8* data = calloc(bytesPerRow, height);
当我在 swift 中转换它时,它返回 "UnsafeMutableRawPointer"。我需要 UInt8.
let data = calloc(bytesPerRow, height)! // UnsafeMutableRawPointer
我尝试过不同的解决方案,但对我没有用。谁能告诉我如何解决这个问题。谢谢
Value of type 'UnsafeMutableRawPointer' has no subscripts
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
Objective 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
您需要为可变指针分配一个类型。在当前状态下,它是指向任何内容的可变指针,但您希望它是指向 UInt8
值缓冲区的可变指针。
为此,只需对您的数据调用 assumingMemoryBound
:
let data = calloc(bytesPerRow, height)!.assumingMemoryBound(to: UInt8.self)
我正在尝试将 objective c 代码转换为 swift。
UInt8* data = calloc(bytesPerRow, height);
当我在 swift 中转换它时,它返回 "UnsafeMutableRawPointer"。我需要 UInt8.
let data = calloc(bytesPerRow, height)! // UnsafeMutableRawPointer
我尝试过不同的解决方案,但对我没有用。谁能告诉我如何解决这个问题。谢谢
Value of type 'UnsafeMutableRawPointer' has no subscripts
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
Objective 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
您需要为可变指针分配一个类型。在当前状态下,它是指向任何内容的可变指针,但您希望它是指向 UInt8
值缓冲区的可变指针。
为此,只需对您的数据调用 assumingMemoryBound
:
let data = calloc(bytesPerRow, height)!.assumingMemoryBound(to: UInt8.self)