gpt4 book ai didi

ios - 我想用 UnsafeMutableRawPointer 偏移量快速编写代码

转载 作者:搜寻专家 更新时间:2023-11-01 07:02:04 27 4
gpt4 key购买 nike

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;
}

swift

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)

此 Objective-C 代码在 Swift 中出错

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

** img

这是我修改代码的错误

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

最佳答案

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

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
}

您没有显示您是如何声明 orArr 的在你的 Swift 代码中,所以我使用 array类型 NSArray就像在您的 Objective-C 代码中一样。

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

RGBAMake的声明也不清楚,但我想我可以假设它是 4 Int s 并返回 UInt32 .

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


还有一些其他的要点:

  • APrimitiveType *在 Objective-C 中应转换为 Swift 中的类型化指针,UnsafePointer<APrimitiveType>UnsafeMutablePointer<APrimitiveType> .使用类型化指针,一些操作如 +用 C 指针规则定义。

  • 一般来说,(APrimitiveType *)malloc(numOfElements * sizeof(APrimitiveType))可以转换为UnsafeMutablePointer<APrimitiveType>.allocate(capacity: numOfElements) .

  • *(aPointer + anInt)相当于aPointer[anInt]在 C 中,后一种语法在 Swift 中工作,具有相同的语法和相同的语义。

关于ios - 我想用 UnsafeMutableRawPointer 偏移量快速编写代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50588357/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com