Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 从cgimage获取位图_Swift_Cgimage - Fatal编程技术网

Swift 从cgimage获取位图

Swift 从cgimage获取位图,swift,cgimage,Swift,Cgimage,我试图从CGImage中获取位图数据,并将其读入像素数组。但是,my imagecontext和imagecontext?.draw()始终返回nil let capturedimage = UIImage(named: "random.jpg") let image = capturedimage?.cgImage let width = Int((image?.width)!) let height = Int((image?.height)!) let bytesPerPixel = 4;

我试图从CGImage中获取位图数据,并将其读入像素数组。但是,my imagecontext和imagecontext?.draw()始终返回nil

let capturedimage = UIImage(named: "random.jpg")
let image = capturedimage?.cgImage
let width = Int((image?.width)!)
let height = Int((image?.height)!)
let bytesPerPixel = 4;
let bytesPerRow = bytesPerPixel * width;
let bitsPerComponent = 8;
let pixelArr: [[UInt32]] = Array(repeating: Array(repeating: 0, count: width), count: height)
let pointer: UnsafeMutableRawPointer = UnsafeMutableRawPointer(mutating: pixelArr);
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue
let imageContext = CGContext(data: pointer, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo);
imageContext?.draw(image!, in: CGRect(x: 0, y: 0, width:width, height:height))
我已经多次检查我的代码,但我不知道哪里出错了。

let image=UIImage(名为:“2.png”)
let image = UIImage(named: "2.png")

guard let cgImage = image?.cgImage else {
    fatalError()
}

let width = cgImage.width
let height = cgImage.height

let bytesPerPixel = 4;
let bytesPerRow = bytesPerPixel * width;
let bitsPerComponent = 8;
let pixelsCount = width * height
let bitmapData: UnsafeMutablePointer<UInt32> = .allocate(capacity: pixelsCount)
defer {
    bitmapData.deallocate()
}
bitmapData.initialize(repeating: 0, count: pixelsCount)
var pixelAtPosition: ((Int, Int) -> UInt32) = { x, y in
    return bitmapData[y * width + x]
}

guard let context = CGContext(data: bitmapData, width: width, height: height,
                              bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow,
                              space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else {
                                fatalError()
}
//draw image to context
var rect = CGRect(x: 0, y: 0, width: width, height: height)
context.draw(cgImage, in: rect)

let traget = pixelAtPosition(200, 200)

print(String(format: "pixel 0x%08X", traget))
print(String(format: "red 0x%2X", (traget & 0x000000FF)))
print(String(format: "green 0x%2X", (traget & 0x0000FF00) >> 8))
print(String(format: "blue 0x%2X", (traget & 0x00FF0000) >> 16))
print(String(format: "alpha 0x%2X", (traget & 0xFF000000) >> 24))
guard let cgImage=image?.cgImage else{ 法塔莱罗() } 让宽度=cgImage.width 让高度=cgImage.height 设字节/像素=4; 让bytesPerRow=bytesPerPixel*宽度; 让bitsPerComponent=8; 设pixelsCount=宽度*高度 让bitmapData:UnsafemeutablePointer=.allocate(容量:像素点) 推迟{ bitmapData.deallocate()文件 } bitmapData.initialize(重复:0,计数:pixelsCount) var pixelAtPosition:((Int,Int)->UInt32)={x,y in 返回位图数据[y*宽度+x] } guard let context=CGContext(数据:位图数据,宽度:宽度,高度:高度, bitsPerComponent:bitsPerComponent,bytesPerRow:bytesPerRow, 空格:CGColorSpaceCreateDeviceRGB(),bitmapInfo:CGImageAlphaInfo.premultipliedLast.rawValue)其他{ 法塔莱罗() } //根据上下文绘制图像 var rect=CGRect(x:0,y:0,宽度:宽度,高度:高度) 绘制(cgImage,in:rect) 设traget=pixelAtPosition(200200) 打印(字符串(格式:“像素0x%08X”,traget)) 打印(字符串(格式:“红色0x%2X”,(traget&0x000000FF))) 打印(字符串(格式:“绿色0x%2X”,(traget&0x0000FF00)>>8)) 打印(字符串(格式:“蓝色0x%2X”(traget&0x00FF0000)>>16)) 打印(字符串(格式:“alpha 0x%2X”,(traget&0xFF000000)>>24))