gpt4 book ai didi

iphone - UIImage 字节顺序已更改

转载 作者:行者123 更新时间:2023-11-29 13:17:37 25 4
gpt4 key购买 nike

<分区>

我正在使用 [uiImage drawAtPoint]绘制新图像。但事实证明颜色变了。我的原图uiImage生成的图像为红色 newImage是蓝色的。这是我的代码:

UIImage* uiImage = [UIHelper loadImage:fileName];
NSString *text = [ApplicationSettings instance].user.name;
UIGraphicsBeginImageContext(uiImage.size);
[uiImage drawAtPoint: CGPointZero];
[text drawAtPoint: CGPointMake(10, 10) withFont: [UIFont systemFontOfSize: 12]];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [Texture createTexture: newImage];

并且在 [Texture createTexture] :

+ (Texture*) createTextureWithImage:(UIImage*)image {
Texture* tex = [[[Texture alloc] init] autorelease];
if ([tex loadImage:image])
return tex;
else {
NSLog(@"Failed to load image %@", image);
return nil;
}
}

- (BOOL)loadImage:(UIImage *)image {
BOOL ret = NO;

if (image) {
// Get the inner CGImage from the UIImage wrapper
CGImageRef cgImage = image.CGImage;

// Get the image size
width = CGImageGetWidth(cgImage);
height = CGImageGetHeight(cgImage);

// Record the number of channels
channels = CGImageGetBitsPerPixel(cgImage)/CGImageGetBitsPerComponent(cgImage);

// Generate a CFData object from the CGImage object (a CFData object represents an area of memory)
CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));

// Copy the image data for use by Open GL
ret = [self copyImageDataForOpenGL: imageData];
CFRelease(imageData);
}

return ret;
}

并且在 [texture copyImageDataForOpenGL]

- (BOOL)copyImageDataForOpenGL:(CFDataRef)imageData {
if (pngData) {
free(pngData);
}

pngData = (unsigned char*)malloc(width * height * channels);
const int rowSize = width * channels;
const unsigned char* pixels = (unsigned char*)CFDataGetBytePtr(imageData);

// Copy the row data from bottom to top
for (int i = 0; i < height; ++i) {
memcpy(pngData + rowSize * i, pixels + rowSize * (height - 1 - i), width * channels);
}

return YES;
}

我猜测生成的图像使用 RGB 顺序,而原始图像使用相反的顺序。这是因为白色和黑色保持不变。

编辑

我正在使用 OpenGL ES 来渲染这个纹理。

return [Texture createTexture: uiImage];而不是 return [Texture createTexture: newImage]工作完美,但我想在 uiImage 上绘制文本.

我测试了UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);并且保存的图像是正确的。

我打印了CGImageGetBitmapInfo(uiImage.CGImage))CGImageGetBitmapInfo(newImage.CGImage) ,而且它们不一样!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com