1

这是我的屏幕截图代码,用于保存在库中或发送电子邮件。问题是,它在模拟器中运行良好,但是当我在设备中运行此代码时,屏幕上的任何内容都只会得到白色图像。我已经尝试过 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);

是否有我正在使用的任何类受模拟器而不是设备支持如果是这样,那么是哪一个,因为就我搜索而言,我没有发现任何类似的东西。

4

2 回答 2

1

我发现我上面的代码是完美的,问题不在于这里。实际上问题在于

eaglLayer.drawableProperties

我已经改变了这段代码

eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:

            [NSNumber numberWithBool:NO],  
                kEAGLDrawablePropertyRetainedBacking, 
                kEAGLColorFormatRGB565, 
                kEAGLDrawablePropertyColorFormat, nil];

对此

eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:

                [NSNumber numberWithBool:YES],  
                    kEAGLDrawablePropertyRetainedBacking, 
                    kEAGLColorFormatRGB565, 
                    kEAGLDrawablePropertyColorFormat, nil];

我刚设置

        kEAGLDrawablePropertyRetainedBacking = YES

感谢上帝,它工作正常。但不知道为什么如果有人知道请告诉我

于 2013-08-11T19:25:04.837 回答
0

试试这个,它在设备上非常适合我:

+ (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;
}
于 2013-08-11T15:57:43.590 回答