我想在swift UnsafeMutableRawPointer偏移量中编写代码

时间:2018-05-29 15:49:48

标签: ios swift

Objective-C代码工作正常。我不知道如何将指针迁移到Swift。

代码Objective-C to * Swift Error in * to UnsafeMutableRawPointer 代码是RGBArray到图像。

//
- (UIImage *)imageWithGrayArray:(NSArray *)array {
    const int width  = 512;
    const int height = 512;

    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;

    UInt32 * pixels = (UInt32 *)malloc(height * width * sizeof(UInt32));
    memset(pixels, 0, height * width * sizeof(UInt32));
    for (int i = 0;  i < array.count;i++ ) {
        NSArray *subArray = array[i];
        for ( int j = 0 ; j < subArray.count; j++) {

          *(pixels +i *width + j) = RGBAMake([subArray[j] intValue],[subArray[j] intValue], [subArray[j] intValue], 255);
        }
    }

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pixels,
                                                 width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
     UIImage *image = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    free(pixels);
    return image;
}

夫特

let width = 512
let height = 512
let  bytesPerPixel = 4
let bytesPerRow = bytesPerPixel * 512
let bitsPerComponent  = 8

var pixels:UnsafeMutableRawPointer = malloc(width*height * MemoryLayout.size(ofValue: UInt32()))

memset(pixels, 0,width * height * MemoryLayout.size(ofValue: UInt32()) )
for i in 0 ..<  orArr.count {
    for j in 0 ..<  orArr[i].count {
        let offset = pixels + i * width + j

pixels.advanced(by: i * width + j).storeBytes(of: RGBMake(r: orArr[i][j] as! Int, g: orArr[i][j]  as! Int, b: orArr[i][j] as! Int, a: 255), as: Int.self)


        }

}
let colorSpace = CGColorSpaceCreateDeviceRGB()

let bufferPointer = UnsafeRawBufferPointer(start: pixels, count: 512*512)
for (index, byte) in bufferPointer.enumerated() {
    print("byte \(index): \(byte)")
}

let content:CGContext = CGContext(data: pixels, width: Int(width), height: Int(height), bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: CGImageAlphaInfo.last.rawValue|CGImageByteOrderInfo.order32Big.rawValue)!
let outPutImage:CGImage = content.makeImage()!

return UIImage(cgImage: outPutImage)

Swift错误中的这个Objective-C代码

*(pixels +i *width + j) = RGBAMake([subArray[j] intValue],[subArray[j] intValue], [subArray[j] intValue], 255);

** img

这是我更改代码的错误

  1. 我可以在链接The Code
  2. 中推送代码

1 个答案:

答案 0 :(得分:2)

我会按如下方式重写您的代码:

func image(withGray array: NSArray) -> UIImage {
    let width = 512
    let height = 512

    let bytesPerPixel = 4
    let bytesPerRow = bytesPerPixel * width
    let bitsPerComponent = 8

    let pixels = UnsafeMutablePointer<UInt32>.allocate(capacity: height * width)
    pixels.initialize(repeating: 0, count: height * width)
    for i in 0..<array.count {
        let subArray = array[i] as! NSArray
        for j in 0..<subArray.count {

            pixels[i*width+j] = RGBAMake(subArray[j] as! Int, subArray[j] as! Int, subArray[j] as! Int, 255)
        }
    }

    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let context = CGContext(data: pixels,
        width: width, height: height,
        bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace,
        bitmapInfo: CGImageAlphaInfo.last.rawValue|CGImageByteOrderInfo.order32Big.rawValue)!
    let image = UIImage(cgImage: context.makeImage()!)

    pixels.deinitialize(count: width * height)
    pixels.deallocate()
    return image
}

您没有在Swift代码中显示您如何声明orArr,因此我在您的Objecive-C代码中使用类型为array的{​​{1}}。

(通常,最好使用Swift Arrays而不是NSArray。)

NSArray的声明也不清楚,但我认为我可以假设它为4 RGBAMake并返回Int

您可能需要修改我的代码以适应您的实际Swift代码。

以及其他一些观点:

    Objective-C中的
  • UInt32应该转换为Swift中的类型指针,APrimitiveType *UnsafePointer<APrimitiveType>。对于类型指针,某些操作如UnsafeMutablePointer<APrimitiveType>是使用C指针规则定义的。

  • 通常,+可以转换为(APrimitiveType *)malloc(numOfElements * sizeof(APrimitiveType))

  • UnsafeMutablePointer<APrimitiveType>.allocate(capacity: numOfElements)相当于C中的*(aPointer + anInt),后一种语法在Swift中使用相同的语法和相同的语义。