gpt4 book ai didi

ios - UIImageWriteToSavedPhotosAlbum 的图像质量

转载 作者:行者123 更新时间:2023-11-29 02:36:00 35 4
gpt4 key购买 nike

我正在尝试从 GLKView 获取屏幕截图并将其保存到照片库。不幸的是,保存的照片质量很低,我不知道为什么。

这是使用 ImageView 类显示在屏幕上的 UIImage 的链接:https://www.dropbox.com/s/d2cyb099n0ndqag/IMG_0148.PNG?dl=0

这是保存在相册中的照片的链接: https://www.dropbox.com/s/8pdaytonwxxd6wk/IMG_0147.JPG?dl=0

代码如下:

- (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];
}

我也尝试了 [GLKView snapshot] 的便捷方法,但我得到了相同的结果。该问题在 iPhone 4、iPhone 3Gs、iPad2 上重现

最佳答案

我通过将图像转换为 PNG 格式并保存最后一张来获得更好的质量:

NSData* imdata =  UIImagePNGRepresentation ( image ); // get PNG representation
UIImage* imgPng = [UIImage imageWithData:imdata]; // wrap UIImage

return imgPng;

关于ios - UIImageWriteToSavedPhotosAlbum 的图像质量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26354428/

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