Search code examples
objective-cios5ios6

Screenshot code working perfectly in Simulator but not in iOS Device


This is my code for screenshot to save in library or do email. The problem is, it is working perfectly in Simulator but when i run this code in Device whatever on screen it only gets white image. I have tried with iOS 5 and iOS 6 both but no luck What should be the reason Where i am wrong

    NSInteger myDataLength = 320 * 430 * 4;
    GLubyte *buffer1 = (GLubyte *) malloc(myDataLength);
    GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);    

    //Read image memory form OpenGL
    glReadPixels(0, 50, 320, 430, GL_RGBA, GL_UNSIGNED_BYTE, buffer1);

    //Invert result image buffer into secondary buffer
    for(int y = 0; y < 430; y++)    {
        for(int x = 0; x < 320 * 4; x++) {
            buffer2[(429 - y) * 320 * 4 + x] = buffer1[y * 4 * 320 + x];
        }
    }

    //Create bitmap context
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef destContext = CGBitmapContextCreate(buffer2, 320, 430, 8, 320 * 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    //Get image from context
    CGImageRef resultContext = CGBitmapContextCreateImage(destContext);
    UIImage *resultImg = [UIImage imageWithCGImage: resultContext];
    CGImageRelease(resultContext);

    //Send to mail or Save to PhotoLibrary
    if (sendMail) {
        [self emailImage: resultImg];
    } else {
        UIImageWriteToSavedPhotosAlbum(resultImg, nil, nil, nil);
    }

    //Release allocated memory
//  [resultImg release];
    free(buffer2);
    free(buffer1);
    CGContextRelease(destContext);
    CGColorSpaceRelease(colorSpace);

is there any class i am using which is supported by Simulator and not the device If it so then which one because as far as i search i found nothing like that.


Solution

  • I have found that My above code is perfect and the issue is not lie here.actually the problem is with

    eaglLayer.drawableProperties

    I have changed from this code

    eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
    
                [NSNumber numberWithBool:NO],  
                    kEAGLDrawablePropertyRetainedBacking, 
                    kEAGLColorFormatRGB565, 
                    kEAGLDrawablePropertyColorFormat, nil];
    

    To this

    eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
    
                    [NSNumber numberWithBool:YES],  
                        kEAGLDrawablePropertyRetainedBacking, 
                        kEAGLColorFormatRGB565, 
                        kEAGLDrawablePropertyColorFormat, nil];
    

    I just set

            kEAGLDrawablePropertyRetainedBacking = YES
    

    And thanks GOD it is working fine. but dont know why if any one know please let me know