我有這個奇怪的問題... 我不得不捕捉屏幕數據,並使用下面的代碼將其轉換爲圖像..此代碼正常工作在iphone/iPad的模擬器和iPhone設備,但不是在iPad上。 iphone設備是具有IOS 3.1.1版和ipad是iOS 4.2的...screenShot代碼不工作在ipad上,在iPhone上工作
- (UIImage *)screenshotImage {
CGRect screenBounds = [[UIScreen mainScreen] bounds];
int backingWidth = screenBounds.size.width;
int backingHeight =screenBounds.size.height;
NSInteger myDataLength = backingWidth * backingHeight * 4;
GLuint *buffer = (GLuint *) malloc(myDataLength);
glReadPixels(0, 0, backingWidth, backingHeight, GL_RGBA4, GL_UNSIGNED_BYTE, buffer);
for(int y = 0; y < backingHeight/2; y++) {
for(int xt = 0; xt < backingWidth; xt++) {
GLuint top = buffer[y * backingWidth + xt];
GLuint bottom = buffer[(backingHeight - 1 - y) * backingWidth + xt];
buffer[(backingHeight - 1 - y) * backingWidth + xt] = top;
buffer[y * backingWidth + xt] = bottom;
}
}
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, myDataLength, releaseScreenshotData);
const int bitsPerComponent = 8;
const int bitsPerPixel = 4 * bitsPerComponent;
const int bytesPerRow = 4 * backingWidth;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(backingWidth,backingHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
// myImage = [self addIconToImage:myImage];
return myImage;}
任何想法怎麼回事錯了??
ķ感謝快速回復..現在我已經改變GL_RGB4到GL_RGBA。代碼在iPhone設備上運行良好,但不是在iPad設備上任何想法是什麼問題....我使用相同的代碼爲這兩個設備 – Tornado
將其更改爲GL_RGBA8,而不僅僅是GL_RGBA(注意'8')。 – datenwolf
嘿我的Xcode沒有給我GL_RGBA8的選項,而是給GL_RGBA8_OES作爲選項..當我使用這個GL_RGBA8_OES它給編譯時錯誤「」GL_RGBA8_OES沒有在這個範圍內定義「」 – Tornado