With OpenGL ES faced for the first time, so there are questions.
The main question: how to create a context on RGBA using OpenGL ES to correctly display transparency in different programs?
I'm trying to figure out the code where there is a problem with correctly saving the png image.
The situation is as follows:
When creating a picture with a transparent background in the application, the created picture after saving to png is displayed in some viewers (on Windows) with a black background, and in some normally.
The transparency of the created images in paint.net is displayed correctly where the pngs I created are displayed with a black background.
I sin on the fact that somewhere there is a flaw in saving the image and there is a problem with the correct preservation of alpha channels (although I could be wrong).
About saving:
- (UIImage *)renderToImage { uint w = (uint)self.frame.size.width; uint h = (uint)self.frame.size.height; NSInteger myDataLength = w * h * 4; GLubyte * buffer = (GLubyte *)malloc((size_t) myDataLength); GLubyte * buffer2 = (GLubyte *)malloc((size_t) myDataLength); __block UIImage * image; runOnMainThread (^{ glFlush(); glBindFramebufferOES(GL_FRAMEBUFFER_OES, _frameBuffer); glBindRenderbufferOES(GL_RENDERBUFFER_OES, _renderBuffer); glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer); for (int y = 0; y < h; y++) { int count = w * 4; for (int x = 0; x < count; x++) { buffer2[(h - 1 - y) * w * 4 + x] = buffer[y * 4 * w + x]; } } CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGContextRef bitmapContext = CGBitmapContextCreate(buffer2, w, h, 8, 4 * w, colorSpaceRef, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault); CGColorSpaceRelease(colorSpaceRef); CGImageRef imageRef = CGBitmapContextCreateImage(bitmapContext); CGContextRelease(bitmapContext); free(buffer); free(buffer2); image = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); }); return image; }
- (NSData *)saveImgChanges { DDLogError(@"%s -- NOT IMPLEMENTED", __FUNCTION__); __block NSData * updatedImageData = nil; runOnMainThread (^{ @autoreleasepool { UIImage * originalImage = [[UIImage alloc] initWithData:[self modifiedAttachmentData]]; CGRect imageRect = CGRectZero; imageRect.size = originalImage.size; UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, [UIScreen mainScreen].scale); [originalImage drawInRect:imageRect]; [originalImage release]; PR_AttachPageInfo * pageInfo = [_attachPagesInfo lastObject]; PR_DrawView * drawView = nil; for (PR_DrawView * dv in _drawViews) { if (dv.tag == [self indexOfPageView:pageInfo.topLevelView]) { drawView = dv; break; } } if (drawView != nil && drawView.hasContent) { UIImage * image = [drawView renderToImage]; [image drawInRect:imageRect]; } UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext(); updatedImageData = UIImagePNGRepresentation(newImage); UIGraphicsEndImageContext(); } }); return updatedImageData; }
Be kind, tell me where the flaw may lurk?
What can cause such problems?
Could they be due to the cant when creating this image?
How can I create context on RGBA?