首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我想写一个快速UnsafeMutableRawPointer偏移量的代码

我想写一个快速UnsafeMutableRawPointer偏移量的代码
EN

Stack Overflow用户
提问于 2018-05-29 23:49:48
回答 1查看 711关注 0票数 1

Objective-C代码运行良好。我不知道如何将指针迁移到Swift。

代码Objective-C to Swift Error in * to UnsafeMutableRawPointer The code is RGBArray to image.

代码语言:javascript
复制
//
- (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;
}

斯威夫特

代码语言:javascript
复制
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代码

代码语言:javascript
复制
*(pixels +i *width + j) = RGBAMake([subArray[j] intValue],[subArray[j] intValue], [subArray[j] intValue], 255);

** img

这是我更改代码时出现的错误

我可以推送链接The Code中的代码

EN

回答 1

Stack Overflow用户

发布于 2018-05-30 03:28:48

我会重写你的代码,如下所示:

代码语言:javascript
复制
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代码中使用NSArray类型的array

(通常,使用Swift数组比使用NSArray更可取。)

RGBAMake的声明也不清楚,但我想我可以假设它接受4个Ints并返回一个UInt32

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

还有一些其他的观点:

Objective-C中的

  • APrimitiveType *应转换为Swift、UnsafePointer<APrimitiveType>UnsafeMutablePointer<APrimitiveType>中的类型化指针。对于类型化指针,一些像+这样的操作是用C-pointer定义的,(APrimitiveType *)malloc(numOfElements * sizeof(APrimitiveType))可以转换为UnsafeMutablePointer<APrimitiveType>.allocate(capacity: numOfElements).
  • *(aPointer + anInt)等同于C中的aPointer[anInt],后一种语法在Swift中使用相同的语法和相同的语义。
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50588357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档