【发布时间】:2012-04-05 05:11:50
【问题描述】:
我按照本教程 (http://www.bit-101.com/blog/?p=1861) 进行操作,发现多次保存同一张图片后,质量会慢慢下降。
除了内存泄漏,这里出了什么问题?它应该为每个像素拉 4 个字节 (rgba)。如果考虑到每个像素,损失在哪里?
----------------- 编辑 -----------------
每次发生顶点位置变换时,我都会从像素数据中保存一个新图像,然后将此更改后的图像加载到我的纹理缓冲区中,并重置顶点/索引缓冲区。这样我就可以保持我的变化持久,并最终减少波涛汹涌的扭曲。请参阅我的其他 SO 问题:OpenGL ES 2.0 Vertex Transformation Algorithms
----------------- 编辑 -----------------
之前
之后
这是教程中的代码:
-(UIImage *) glToUIImage {
NSInteger myDataLength = 320 * 480 * 4;
// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for(int y = 0; y < 480; y++)
{
for(int x = 0; x < 320 * 4; x++)
{
buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + x];
}
}
// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * 320;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
// then make the uiimage from that
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
return myImage;
}
-(void)captureToPhotoAlbum {
UIImage *image = [self glToUIImage];
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
}
【问题讨论】:
-
当您说“多次”时,您的意思是在同一张图片上多次运行此代码,还是从相册中加载图片然后再次保存?如果是后者,那么我猜你看到的是 JPEG 压缩(我对 iOS 不熟悉,所以我不能确定图像是否被写为 JPEG)。
-
每次发生顶点位置变换时,我都会从像素数据中保存一个新图像,然后将此更改后的图像加载到我的纹理缓冲区中,并重置顶点/索引缓冲区。这样我就可以保持我的变化持久,并最终减少波涛汹涌的扭曲。请参阅我的其他 SO 问题:stackoverflow.com/questions/9777393/…
-
这是关键信息——您应该将其添加到您的问题中。
-
此外,包含展示质量损失的前后图像会非常有用 - 看到问题将有助于我们识别和理解它。
标签: ios image-processing opengl-es-2.0 image-quality