Ios 非女性化指针<;UInt32>;。在swift 3中读取图像时,指针对象总是给出相同的值
我有下面的代码,我试图准备好我的图像,以便定义白色像素的位置,透明等等,但是我的输出总是255(如果(pointer.pointee>>24)==255),所以它告诉我它是全黑的,而不是! 有什么帮助吗Ios 非女性化指针<;UInt32>;。在swift 3中读取图像时,指针对象总是给出相同的值,ios,swift,unsafemutablepointer,Ios,Swift,Unsafemutablepointer,我有下面的代码,我试图准备好我的图像,以便定义白色像素的位置,透明等等,但是我的输出总是255(如果(pointer.pointee>>24)==255),所以它告诉我它是全黑的,而不是! 有什么帮助吗 var colorSpace = CGColorSpaceCreateDeviceRGB() let imageCG = image.cgImage! imageWidth = imageCG.width imageHeight = imageCG.height
var colorSpace = CGColorSpaceCreateDeviceRGB()
let imageCG = image.cgImage!
imageWidth = imageCG.width
imageHeight = imageCG.height
imageScale = image.scale
totalPixels = imageHeight * imageWidth
var bitsPerComponent = 8
var bytesPerRow = 4 * imageWidth
var bitmapInfo = CGBitmapInfo.byteOrder32Little.rawValue | CGImageAlphaInfo.premultipliedFirst.rawValue
// Init imageContext
imageContext = CGContext(data: nil, width: imageWidth, height: imageHeight, bitsPerComponent: bitsPerComponen`enter code here`t, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo)!
imageContext.draw(imageCG, in: CGRect(x: 0, y: 0, width: imageWidth, height: imageHeight))
// Init imageData
imageData = imageContext.data!.bindMemory(to: UInt32.self, capacity: totalPixels)
// Init regionsData
regionsData = UnsafeMutablePointer<UInt16>.allocate(capacity: totalPixels)
var regionsPointer: UnsafeMutablePointer<UInt16> = regionsData
var pointer: UnsafeMutablePointer<UInt32> = imageData
// Scan image data and mark full transparent, semitransparent and opaque pixels in regions data
let minAlpha = UInt32(255 - transparencyTolerance)
for _ in 0..<totalPixels {
if (pointer.pointee >> 24) == 255 {
regionsPointer.pointee = regionsBlackColor
} else if (pointer.pointee >> 24) > minAlpha {
regionsPointer.pointee = regionsTransparentColor
} else {
regionsPointer.pointee = regionsWhiteColor
}
pointer.pointee = whiteColor
pointer = pointer.successor()
regionsPointer = regionsPointer.successor()
}
var colorSpace=CGColorSpaceCreateDeviceRGB()
让imageCG=image.cgImage!
imageWidth=imageCG.width
imageHeight=imageCG.height
imageScale=image.scale
totalPixels=图像高度*图像宽度
变量bitsPerComponent=8
var bytesPerRow=4*imageWidth
var bitmapInfo=CGBitmapInfo.byteOrder32Little.rawValue | CGImageAlphaInfo.PremultipledFirst.rawValue
//Init imageContext
imageContext=CGContext(数据:nil,宽度:imageWidth,高度:imageHeight,bitsPerComponent:bitsPerComponent`在此处输入代码`t,bytesPerRow:bytesPerRow,空格:colorSpace,bitmapInfo:bitmapInfo)!
draw(imageCG,in:CGRect(x:0,y:0,width:imageWidth,height:imageHeight))
//初始化图像数据
imageData=imageContext.data!。bindMemory(至:UInt32.self,容量:totalPixels)
//初始区域数据
regionsData=UnsafeMutablePointer.allocate(容量:totalPixels)
变量regionspoint:UnsafeMutablePointer=regionsData
变量指针:unsafemeutablepointer=imageData
//扫描图像数据并在区域数据中标记全透明、半透明和不透明像素
让minAlpha=UInt32(255-透明细胞耐受性)
对于uu0..>24)==255{
regionsPointer.pointee=regionsBlackColor
}else if(pointer.pointee>>24)>minAlpha{
regionsPointer.pointee=区域透明颜色
}否则{
regionspoint.pointee=regionswitecolor
}
pointer.pointee=白色
指针=指针。后续()
regionsPointer=regionsPointer.successiver()
}
255的alpha值并不意味着黑色,它只是意味着不透明。您需要查看RGB值,以确定给定像素是否为黑色。谢谢您的回答,但我的问题是它总是返回255(无论255是什么意思),即使我遍历图像中的所有像素,我也会得到相同的值吗?您的图像不透明吗?不是,是这样的,如果你可以访问此链接:该图像是不透明的,它有纯白背景。