下面的代码是尝试获取图像,将其组件提取到vImage_Buffer中,然后遍历此缓冲区并抓取每个组件并通过填充outputBuffer反转红色和蓝色通道。之后,我想获取生成的outputBuffer并从中创建一个UIImage。
即使以下代码编译并运行正常,outputImage仍然是空白的,我没有收到任何错误。
CGFloat inputImageScale = inputImage.scale;
CGSize outputImageSizeInPoints = inputImage.size;
CGRect outputImageRectInPoints = { CGPointZero, outputImageSizeInPoints };
UIGraphicsBeginImageContextWithOptions(outputImageRectInPoints.size, NO, inputImageScale);
CGContextRef outputContext = UIGraphicsGetCurrentContext();
CGContextScaleCTM(outputContext, 1.0, -1.0);
CGContextTranslateCTM(outputContext, 0, -outputImageRectInPoints.size.height);
vImage_Buffer originalBuffer;
vImage_Buffer *inputBuffer;
vImage_Buffer *outputBuffer;
vImage_CGImageFormat format = {
.bitsPerComponent = 8,
.bitsPerPixel = 32,
.colorSpace = NULL,
// (kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little)
// requests a BGRA buffer.
.bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little,
.version = 0,
.decode = NULL,
.renderingIntent = kCGRenderingIntentDefault
};
vImage_Error e = vImageBuffer_InitWithCGImage(&originalBuffer, &format, NULL, inputImage.CGImage, kvImagePrintDiagnosticsToConsole);
if (e != kvImageNoError)
{
NSLog(@"*** error: vImageBuffer_InitWithCGImage returned error code %zi for inputImage: %@", e, inputImage);
UIGraphicsEndImageContext();
return nil;
}
inputBuffer = &originalBuffer;
vImageBuffer_Init(outputBuffer, originalBuffer.height, originalBuffer.width, format.bitsPerPixel, kvImageNoFlags);
Byte *data = (Byte*)inputBuffer->data;
for(vImagePixelCount i = 0; i < inputBuffer->width + inputBuffer->height; i+=4) {
Byte B = (Byte)data[i];
Byte G = (Byte)data[i + 1];
Byte R = (Byte)data[i + 2];
Byte A = (Byte)data[i + 3];
NSLog(@"1: (%d) %d, %d, %d, %d", i, R,G,B,A);
((Byte*)outputBuffer->data)[i] = R;
((Byte*)outputBuffer->data)[i + 1] = G;
((Byte*)outputBuffer->data)[i + 2] = B;
((Byte*)outputBuffer->data)[i + 3] = A;
}
CGImageRef effectCGImage = vImageCreateCGImageFromBuffer(outputBuffer, &format, &cleanupBuffer, NULL, kvImageNoFlags, &e);
// draw effect image
CGContextSaveGState(outputContext);
CGContextDrawImage(outputContext, outputImageRectInPoints, effectCGImage);
CGContextRestoreGState(outputContext);
// Cleanup
CGImageRelease(effectCGImage);
// Output image is ready.
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
那么,上面的代码有什么问题?
P.S。:我知道对于反转通道,我可以使用一些vImage_函数,但这不是我想要实现的。我想要实现的是通过逐个改变每个像素来改变图像。最终我的目的是创建一个DisplacementMapFilter(根据提供的地图转换图像像素)。
答案 0 :(得分:2)
找出问题所在。这种情况下的特殊问题是:
for(vImagePixelCount i = 0; i&lt;(inputBuffer-&gt; width * inputBuffer-&gt; height * 4); i + = 4)
我只是通过缓冲区的一小部分循环。现在我循环遍历整个缓冲区。