Objective-C代码运行良好。我不知道如何将指针迁移到Swift。
代码Objective-C to Swift Error in * to UnsafeMutableRawPointer The code is RGBArray to image.
//
- (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
这是我更改代码时出现的错误
我可以推送链接The Code中的代码
发布于 2018-05-30 03:28:48
我会重写你的代码,如下所示:
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个Int
s并返回一个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中使用相同的语法和相同的语义。https://stackoverflow.com/questions/50588357
复制相似问题