这是我保存在图书馆或电子邮件中的截图代码。问题是,它在Simulator中完美运行,但是当我在设备中运行此代码时,屏幕上只显示白色图像。我已尝试使用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);
有没有我正在使用的类,Simulator支持而不是设备如果那么那么哪一个,因为据我搜索我发现没有那样的。
答案 0 :(得分: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
感谢上帝的工作正常。 但不知道为什么如果有人知道请告诉我
答案 1 :(得分: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;
}