UIImageWriteToSavedPhotosAlbum的图像质量

时间:2014-10-14 06:56:11

标签: ios objective-c opengl-es

我正在尝试从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快照]的便捷方法,但我得到了相同的结果。问题在iPhone 4,iPhone 3Gs,IPad2

上复制

1 个答案:

答案 0 :(得分:1)

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

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

return imgPng;