Ios 像素颜色错误仅适用于iPhone 6模拟器

Ios 像素颜色错误仅适用于iPhone 6模拟器,ios,swift,pixel,rgba,iphone-6,Ios,Swift,Pixel,Rgba,Iphone 6,我需要读取图像中某一点的像素颜色,我的代码在模拟器中适用于除iPhone 6以外的所有iPhone(包括iPhone 6 Plus) 我不知道为什么,但我猜像素的索引不正确,因为它在错误的位置检测到颜色。 谢谢你的帮助。 这是我的密码 UIGraphicsBeginImageContext(upperCaseView.frame.size) upperCaseView.layer.renderInContext(UIGraphicsGetCurrentContext()!) snapshotIm

我需要读取图像中某一点的像素颜色,我的代码在模拟器中适用于除iPhone 6以外的所有iPhone(包括iPhone 6 Plus)

我不知道为什么,但我猜像素的索引不正确,因为它在错误的位置检测到颜色。 谢谢你的帮助。 这是我的密码

UIGraphicsBeginImageContext(upperCaseView.frame.size)
upperCaseView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
snapshotImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

let pixelData = CGDataProviderCopyData(CGImageGetDataProvider(snapshotImage.CGImage))
let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

let pixelInfo: Int = ((Int(snapshotImage.size.width) * Int(point.y)) + Int(point.x)) * 4

print(data[pixelInfo])
print(data[pixelInfo+1])
print(data[pixelInfo+2])
UIGraphicsBeginImageContext(upperCaseView.frame.size)
upperCaseView.layer.RenderContext(UIGraphicsGetCurrentContext()!)
snapshotImage=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
让pixelData=CGDataProviderCopyData(CGImageGetDataProvider(snapshotImage.CGImage))
let data:UnsafePointer=CFDataGetBytePtr(像素数据)
设pixelInfo:Int=((Int(snapshotImage.size.width)*Int(point.y))+Int(point.x))*4
打印(数据[pixelInfo])
打印(数据[pixelInfo+1])
打印(数据[pixelInfo+2])

非常感谢斯科特·汤普森


我用CGImageGetBytesPerRow代替了图像宽度,现在它可以工作了。 正确的代码如下:

    UIGraphicsBeginImageContext(upperCaseView.frame.size)
    upperCaseView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    snapshotImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    let pixelData = CGDataProviderCopyData(CGImageGetDataProvider(snapshotImage.CGImage))
    let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

    let bytesPerPixel = (CGImageGetBitsPerPixel(snapshotImage.CGImage) / 8)
    let bytesPerRow = CGImageGetBytesPerRow(snapshotImage.CGImage)
    let pixelInfo: Int = (bytesPerRow * Int(point.y)) + (Int(point.x) * bytesPerPixel)

    print(data[pixelInfo])
    print(data[pixelInfo+1])
    print(data[pixelInfo+2])
UIGraphicsBeginImageContext(upperCaseView.frame.size)
upperCaseView.layer.RenderContext(UIGraphicsGetCurrentContext()!)
snapshotImage=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
让pixelData=CGDataProviderCopyData(CGImageGetDataProvider(snapshotImage.CGImage))
let data:UnsafePointer=CFDataGetBytePtr(像素数据)
让bytesPerPixel=(CGImageGetBitsPerPixel(snapshotImage.CGImage)/8)
让bytesPerRow=CGImageGetBytesPerRow(snapshotImage.CGImage)
设pixelInfo:Int=(bytesPerRow*Int(point.y))+(Int(point.x)*bytesPerPixel)
打印(数据[pixelInfo])
打印(数据[pixelInfo+1])
打印(数据[pixelInfo+2])

为工作和非工作设备提供
点.y
点.x
值。您确定您的图像数据在所有平台上都以正确的像素格式编码吗?您是自己定义图像格式,还是依靠操作系统为您选择图像格式?一般来说,你不应该依赖图像的宽度来告诉你每行有多少字节,因为图像数据有时包括,而应该使用CGImageGetBytesPerRow。图像是整个屏幕的快照(我在帖子中添加了代码),对于除iPhone 6以外的所有模拟器,它适用于屏幕上的任何点。对于iPhone 6模拟器,它似乎不起作用。我用CGImageGetBytesPerRow代替了图像宽度,它现在起作用了,非常感谢你的提示。