-(UIImage *) glToUIImage
{
int width = [self.view bounds].size.width;
int height = [self.view bounds].size.height;
NSInteger myDataLength = width * height * 4;
// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for(int y = 0; y <height; y++)
{
for(int x = 0; x <width * 4; x++)
{
buffer2[(height -1 - y) * width * 4 + x] = buffer[y * 4 * width + x];
}
}
// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
// then make the uiimage from that
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
return myImage;
}
//-(UIImage *)getImgeFromTwoPic
//{
// UIGraphicsBeginImageContext(self.view.bounds.size);
// [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
// UIImage *uiImageOne = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
//
// UIImageWriteToSavedPhotosAlbum(uiImageOne, self, nil, nil);
//
// UIImage *imgeTwo = [self screenshot];
// [self saveimge:imgeTwo];
//
// NSLog(@"%f %f",imgeTwo.size.width,imgeTwo.size.height);
// return [self mergerImage:uiImageOne secodImage:imgeTwo];
//}
-(UIImage *) screenshot
{
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
//- (void)saveToDisk:(UIImage *)image{
// NSString *dir = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//
// NSLog(@"保存路径:%@",dir);
//
// NSString *path = [NSString stringWithFormat:@"%@/pic_%f.png",dir,[NSDate timeIntervalSinceReferenceDate]];
// NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];
// [data writeToFile:path atomically:YES];
//
// NSLog(@"保存完毕");
//}
//- (void)saveimge:(UIImage *)imge
//{
//
// UIImageWriteToSavedPhotosAlbum(imge, self, nil, nil);
// NSLog(@"结束");
//}
//-(UIImage *)mergerImage:(UIImage *)firstImage secodImage:(UIImage *)secondImage
//{
//
// CGSize imageSize = CGSizeMake(320, 480);
// UIGraphicsBeginImageContextWithOptions(imageSize, NO, 4.0f);
//
// [firstImage drawInRect:CGRectMake(0, 0, firstImage.size.width, firstImage.size.height)];
// [secondImage drawInRect:CGRectMake(0, 64+40, secondImage.size.width, secondImage.size.height)];
//
// UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
//
// return resultImage;
//}