Objective c 屏幕截图代码在模拟器中运行良好,但在iOS设备中无法运行
这是我的截图代码,可以保存在库中或发送电子邮件。问题是,它在模拟器中工作得很好,但当我在屏幕上的任何设备上运行此代码时,它只会得到白色图像。我尝试过iOS 5和iOS 6,但都没有成功 我错的原因应该是什么Objective c 屏幕截图代码在模拟器中运行良好,但在iOS设备中无法运行,objective-c,ios5,ios6,Objective C,Ios5,Ios6,这是我的截图代码,可以保存在库中或发送电子邮件。问题是,它在模拟器中工作得很好,但当我在屏幕上的任何设备上运行此代码时,它只会得到白色图像。我尝试过iOS 5和iOS 6,但都没有成功 我错的原因应该是什么 NSInteger myDataLength = 320 * 430 * 4; GLubyte *buffer1 = (GLubyte *) malloc(myDataLength); GLubyte *buffer2 = (GLubyte *) malloc(myD
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);
NSInteger myDataLength=320*430*4;
GLubyte*buffer1=(GLubyte*)malloc(myDataLength);
GLubyte*buffer2=(GLubyte*)malloc(myDataLength);
//从OpenGL读取图像存储器
glReadPixels(0、50、320、430、GL_RGBA、GL_无符号字节、buffer1);
//将结果图像缓冲区反转为次缓冲区
对于(int y=0;y<430;y++){
对于(int x=0;x<320*4;x++){
buffer2[(429-y)*320*4+x]=buffer1[y*4*320+x];
}
}
//创建位图上下文
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
CGContextRef destContext=CGBitmapContextCreate(缓冲区2、320、430、8、320*4,颜色空间,KCGIMAGEAlphaPremultipledLast | kCGBitmapByteOrder32Big);
//从上下文中获取图像
CGImageRef resultContext=CGBitmapContextCreateImage(destContext);
UIImage*resultImg=[UIImage imageWithCGImage:resultContext];
CGImageRelease(resultContext);
//发送到邮件或保存到照片库
如果(发送邮件){
[self-emailImage:resultImg];
}否则{
UIImageWriteToSavedPhotosAlbum(结果,无,无,无);
}
//释放分配的内存
//[结果];
免费(2);
免费(1);
CGContextRelease(destContext);
CGCOLORSPACTERELEASE(色彩空间);
我使用的任何类都是模拟器支持的,而不是设备支持的,如果是,那么是哪一个,因为就我搜索而言,我没有发现类似的类。试试这个,它在设备上非常适合我:
+ (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;
}
试试这个,它在设备上非常适合我:
+ (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;
}
我发现我上面的代码是完美的,问题不在这里。实际上问题在于
EagleLayer.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
感谢上帝,它工作得很好。
但是不知道为什么如果有人知道,请告诉我我发现我上面的代码是完美的,问题不在这里。实际上问题在于
EagleLayer.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
感谢上帝,它工作得很好。
但是不知道为什么如果有人知道,请让我知道兄弟这不是解决我的问题兄弟这不是解决我的问题