私は[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)
ましたが、同じではありません。