简体   繁体   中英

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.

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

try this one, it works perfect for me on the device:

+ (UIImage *)imageFromView:(UIView *)view inRect:(CGRect)rect {
    CGFloat screenScale = [[UIScreen mainScreen] scale];
    if ([view respondsToSelector:@selector(contentSize)]) {
        UIScrollView *scrollView = (UIScrollView *)view;
        UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, NO, screenScale);
    } else {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, screenScale);
    }
    CGContextRef resizedContext = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:resizedContext];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    image = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(image.CGImage, CGRectMake(rect.origin.x*screenScale, rect.origin.y*screenScale, rect.size.width*screenScale, rect.size.height*screenScale))];
    UIGraphicsEndImageContext();
    return [image imageScaledToSize:CGSizeMake(rect.size.width, rect.size.height)];
}

- (UIImage *)imageScaledToSize:(CGSize)newSize {
    if ((self.size.width == newSize.width) && (self.size.height == newSize.height)) {
        return self;
    }
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM