Ios UIImageWriteToSavedPhotosAlbum的图像质量
我正在尝试从GLKView中获取一个屏幕截图,并将其保存到照片库中。不幸的是,保存的照片质量很低,我不知道为什么 这是指向使用ImageView类在屏幕上显示的UIImage的链接: 这是保存在相册中的照片的链接: 代码如下:Ios UIImageWriteToSavedPhotosAlbum的图像质量,ios,objective-c,opengl-es,Ios,Objective C,Opengl Es,我正在尝试从GLKView中获取一个屏幕截图,并将其保存到照片库中。不幸的是,保存的照片质量很低,我不知道为什么 这是指向使用ImageView类在屏幕上显示的UIImage的链接: 这是保存在相册中的照片的链接: 代码如下: - (UIImage*)snapshot { GLint backingWidth, backingHeight; GLint framebuff; glGetIntegerv( GL_FRAMEBUFFER_BINDING, &fr
- (UIImage*)snapshot
{
GLint backingWidth, backingHeight;
GLint framebuff;
glGetIntegerv( GL_FRAMEBUFFER_BINDING, &framebuff);
glBindFramebuffer(GL_FRAMEBUFFER, framebuff);
// Get the size of the backing CAEAGLLayer
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);
NSInteger x = 0, y = 0, width = backingWidth, height = backingHeight;
NSInteger dataLength = width * height * 4;
GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte));
// Read pixel data from the framebuffer
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
GLenum errCode;
if ((errCode = glGetError()) != GL_NO_ERROR) {
//errStr = gluErrorString(errCode);
printf("%u: OpenGL ERROR ",errCode);
}
// 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(dataLength);
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width * 4; x++)
{
buffer2[((height - 1) - y) * width * 4 + x] = data[y * 4 * width + x];
}
}
// Create a CGImage with the pixel data
// If your OpenGL ES content is opaque, use kCGImageAlphaNoneSkipLast to ignore the alpha channel
// otherwise, use kCGImageAlphaPremultipliedLast
CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, buffer2, dataLength, NULL);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGImageRef iref = CGImageCreate(width, height, 8, 32, width * 4, colorspace, kCGBitmapByteOrderDefault, ref, NULL, YES, kCGRenderingIntentDefault);
// Retrieve the UIImage from the current context
UIImage *image = [UIImage imageWithCGImage:iref];
UIImageView *myImageView = [[UIImageView alloc] init];
myImageView.frame = CGRectMake(0, 0, fluid.gpu.Drawing.Pong->width,fluid.gpu.Drawing.Pong->height);
myImageView.image = image;
[self.view addSubview:myImageView];
CFRelease(colorspace);
CGImageRelease(iref);
free(data);
return image;
}
-(void)SavePhoto{
UIImage *temp = [self snapshot];
UIImageWriteToSavedPhotosAlbum(temp, self, @selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), (__bridge void*)self.context);
}
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
NSString *message;
NSString *title;
if (!error) {
title = NSLocalizedString(@"Save picture", @"");
message = NSLocalizedString(@"Your screenshot was saved to Photos Album.", @"");
} else {
title = NSLocalizedString(@"There was an error saving screenshot.", @"");
message = [error description];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", @"")
otherButtonTitles:nil];
[alert show];
}
-(UIImage*)快照
{
闪烁背宽,背高;
闪烁的帧增益;
glGetIntegerv(GL\u帧缓冲区\u绑定和framebuff);
glBindFramebuffer(GL_FRAMEBUFFER,framebuff);
//获取支持caeaglayer的大小
GLGetRenderBuffer参数(GL_RENDERBUFFER、GL_RENDERBUFFER_WIDTH和backingWidth);
GLGetRenderBuffer参数(GL_RENDERBUFFER、GL_RENDERBUFFER_高度和背景高度);
NSInteger x=0,y=0,width=backingWidth,height=backingHeight;
NSInteger数据长度=宽度*高度*4;
GLubyte*data=(GLubyte*)malloc(dataLength*sizeof(GLubyte));
//从帧缓冲区读取像素数据
glPixelStorei(GLU包装校准,4);
glReadPixels(x、y、宽度、高度、GL_RGBA、GL_无符号字节、数据);
格伦编码;
如果((errCode=glGetError())!=GL\u无错误){
//errStr=gluErrorString(errCode);
printf(“%u:OpenGL错误”,errCode);
}
//gl呈现“倒置”,以便从上到下交换到新阵列中。
//肯定有更好的办法,但这是可行的。
GLubyte*buffer2=(GLubyte*)malloc(数据长度);
对于(int y=0;ywidth,fluid.gpu.Drawing.Pong->height);
myImageView.image=图像;
[self.view addSubview:myImageView];
CFRelease(色彩空间);
CGImageRelease(iref);
免费(数据);
返回图像;
}
-(作废)保存照片{
UIImage*temp=[自快照];
UIImageWriteToSavedPhotosAlbum(临时,自我,@selector(ImageSavedPhotosAlbum:didFinishSavingWithError:contextInfo:),(uu桥空*)self.context);
}
-(void)ImageSavedPhotosAlbum:(UIImage*)图像未完成SaveingWitherRor:(NSError*)错误上下文信息:(void*)上下文信息{
NSString*消息;
NSString*标题;
如果(!错误){
title=NSLocalizedString(@“保存图片”,“@”);
message=NSLocalizedString(@“您的屏幕截图已保存到相册中。”,@”);
}否则{
title=NSLocalizedString(@“保存屏幕截图时出错。”,@“”);
消息=[错误描述];
}
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:title
信息:信息
代表:无
CancelButtonTile:NSLocalizedString(@“确定”,@“”)
其他按钮:无];
[警报显示];
}
我还尝试了[GLKView snapshot]的便捷方法,但得到了相同的结果。这个问题在iPhone4、iPhone3GS和IPad2上都有,我通过将图像转换为PNG格式并保存最后一张来获得更好的质量:
NSData* imdata = UIImagePNGRepresentation ( image ); // get PNG representation
UIImage* imgPng = [UIImage imageWithData:imdata]; // wrap UIImage
return imgPng;
@genpfault-为什么这个问题被否决了?是因为内容还是因为标签?我想知道我做错了什么。GLKView快照上的颜色有问题,保存为PNG解决了我的问题-谢谢!