Hi, ...There is another thread here which marks this code as the final answer but in the latest api 10.1, this just results in a blank image which is quite confusing for me.I would appreciate if some one can help me deal with this.thanks and RegardsH Akhtar
CAEAGLLayer *eaglLayer = (CAEAGLLayer *) theMap.layer; eaglLayer.drawableProperties = @{ kEAGLDrawablePropertyRetainedBacking: [NSNumber numberWithBool:YES], kEAGLDrawablePropertyColorFormat: kEAGLColorFormatRGBA8 };
- (NSData *)getImage{ //the rest GLint width; GLint height; glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width); glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &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); //CGImageRef imageRefCopy = CGImageCreateCopy(imageRef); // then make the uiimage from that UIImage *myImage = [[UIImage alloc] initWithCGImage:imageRef]; NSData *iDat = UIImageJPEGRepresentation(myImage, .5); CGImageRelease(imageRef); CGDataProviderRelease(provider); CGColorSpaceRelease(colorSpaceRef); free(buffer); free(buffer2); return iDat; }
Thanks a lot for this... However this only works in the simulator but the same code returns a blank screen in the device.Any idea why this might be.\Thanks again
Hey Guys,I have followed the above example and tried moving the renderers around. Like some of the other comments the code works great in the simulator but when I put it onto my iPad, I get a black image. Has anyone figured this out? I am sure I am just doing something wrong.Cheers,Jeff
-(void) PrintPermitFinal { CAEAGLLayer *eaglLayer = (CAEAGLLayer *) self.mapView.layer; eaglLayer.drawableProperties = @{ kEAGLDrawablePropertyRetainedBacking: [NSNumber numberWithBool:YES], kEAGLDrawablePropertyColorFormat: kEAGLColorFormatRGBA8 }; // reset buttons [selfsettoInitialMapView]; // create image and pdf UIImage *screenshot = [[UIImage alloc] initWithData:[self getImage]]; // use clip path Rect to clip image to right size CGImageRef imageref = CGImageCreateWithImageInRect([screenshot CGImage], clipRect); screenshot = [UIImage imageWithCGImage:imageref]; //save image to photo album for testing only UIImageWriteToSavedPhotosAlbum(screenshot,self,@selector(image:didFinishSavingWithError:contextInfo:),nil); //Send Image to Dictionary if (AppDelagate.PermitImages == nil) { AppDelagate.PermitImages = [[NSMutableDictionaryalloc] init]; } NSString *Key= [selfCreatePermitID:[AppDelagate.GraphicToPrint.allAttributesvalueForKey:@Full_Name] AndAddress:[AppDelagate.GraphicToPrint.allAttributesvalueForKey:@Owner_Address]]; [AppDelagate.PermitImages setObject:screenshot forKey:Key]; self.tabBarController.selectedViewController = [self.tabBarController.viewControllersobjectAtIndex:1]; }
- (NSData *)getImage { GLint width; GLint height; glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width); glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &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 alloc] initWithCGImage:imageRef]; NSData *iDat = UIImageJPEGRepresentation(myImage, 0); CGImageRelease(imageRef); CGDataProviderRelease(provider); CGColorSpaceRelease(colorSpaceRef); free(buffer); free(buffer2); return iDat; }
Humza,I have tried that but maybe I am putting it in the wrong spot or doing something else wrong. I am attaching my code for both calling the get image process and the process itself. Any help would be greatly appreciated. Calling -(void) PrintPermitFinal { CAEAGLLayer *eaglLayer = (CAEAGLLayer *) self.mapView.layer; eaglLayer.drawableProperties = @{ kEAGLDrawablePropertyRetainedBacking: [NSNumber numberWithBool:YES], kEAGLDrawablePropertyColorFormat: kEAGLColorFormatRGBA8 }; // reset buttons [selfsettoInitialMapView]; // create image and pdf UIImage *screenshot = [[UIImage alloc] initWithData:[self getImage]]; // use clip path Rect to clip image to right size CGImageRef imageref = CGImageCreateWithImageInRect([screenshot CGImage], clipRect); screenshot = [UIImage imageWithCGImage:imageref]; //save image to photo album for testing only UIImageWriteToSavedPhotosAlbum(screenshot,self,@selector(image:didFinishSavingWithError:contextInfo:),nil); //Send Image to Dictionary if (AppDelagate.PermitImages == nil) { AppDelagate.PermitImages = [[NSMutableDictionaryalloc] init]; } NSString *Key= [selfCreatePermitID:[AppDelagate.GraphicToPrint.allAttributesvalueForKey:@Full_Name] AndAddress:[AppDelagate.GraphicToPrint.allAttributesvalueForKey:@Owner_Address]]; [AppDelagate.PermitImages setObject:screenshot forKey:Key]; self.tabBarController.selectedViewController = [self.tabBarController.viewControllersobjectAtIndex:1]; } Getting the image virtually unchanged from Bernese - (NSData *)getImage { GLint width; GLint height; glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width); glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &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 alloc] initWithCGImage:imageRef]; NSData *iDat = UIImageJPEGRepresentation(myImage, 0); CGImageRelease(imageRef); CGDataProviderRelease(provider); CGColorSpaceRelease(colorSpaceRef); free(buffer); free(buffer2); return iDat; } Cheers,Jeff
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.