简体   繁体   English

屏幕截图代码可以在Simulator中完美运行,但不能在iOS Device中运行

[英]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. 问题是,它在Simulator中运行良好,但是当我在Device中运行此代码时,无论在屏幕上什么,它只会显示白色图像。 I have tried with iOS 5 and iOS 6 both but no luck What should be the reason Where i am wrong 我已经尝试过同时使用iOS 5和iOS 6,但没有运气我应该错了的原因是什么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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