Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying a screen shot generated UIImage is not displaying in UIImageView (for device only)
    text
    copied!<p>I am trying to save an OpenGL buffer (whats currently displayed in the view) to the device's photo library. The code snippet below works fine on the simulator. But for the actual device it is crashing. I believe there could be a problem with the way im creating the UIImage captured from the screen.</p> <ul> <li>This operations is initiated via an IBAction event handle method.</li> <li>The function i use to save the image is UIImageWriteToSavedPhotosAlbum (i recently changed this to ALAssetsLibrary's writeImageToSavedPhotosAlbum).</li> <li>I have ensured that my app is authorized to access the Photos library.</li> <li>I also made sure that my CGImageRed is globally defined (defined at the top of the file) and my UIImage is a (nonatomic, retain) property.</li> </ul> <p>Can somebody help me fix this issue? I'd like to have a valid UIImage reference that was generated from the glReadPixels data.</p> <p>Below is the relevant code snippet (call to save to photo library):</p> <pre><code>-(void)TakeImageBufferSnapshot:(CGSize)dimensions { NSLog(@"TakeSnapShot 1 : (%f, %f)", dimensions.width, dimensions.height); NSInteger size = dimensions.width * dimensions.height * 4; GLubyte *buffer = (GLubyte *) malloc(size); glReadPixels(0, 0, dimensions.width, dimensions.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); GLubyte *buffer2 = (GLubyte *) malloc(size); int height = (int)dimensions.height - 1; int width = (int)dimensions.width; for(int y = 0; y &lt; dimensions.height; y++) { for(int x = 0; x &lt; dimensions.width * 4; x++) { buffer2[(height - 1 - y) * width * 4 + x] = buffer[y * 4 * width + x]; } } NSLog(@"TakeSnapShot 2"); // make data provider with data. CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, size, NULL); if (buffer) free(buffer); if (buffer2) free(buffer2); // prep the ingredients int bitsPerComponent = 8; int bitsPerPixel = 32; int bytesPerRow = 4 * self.view.bounds.size.width; CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; NSLog(@"TakeSnapShot 3"); // make the cgimage g_savePhotoImageRef = CGImageCreate(dimensions.width, dimensions.height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); NSLog(@"TakeSnapShot 4"); // then make the uiimage from that self.savePhotoImage = [UIImage imageWithCGImage:g_savePhotoImageRef]; CGColorSpaceRelease(colorSpaceRef); CGDataProviderRelease(provider); } -(void)SaveToPhotoAlbum { ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; NSLog(@"Authorization status: %d", status); if (status == ALAuthorizationStatusAuthorized) { [self TakeImageBufferSnapshot:self.view.bounds.size]; // UPDATED - DO NOT proceed to save to album below. // Instead, set the created image to a UIImageView IBOutlet. // On the simulator this shows the screen/buffer captured image (as expected) - // but on the device (ipad) this doesnt show anything and the app crashes. self.testImageView.image = self.savePhotoImage; return; NSLog(@"Saving to photo album..."); UIImageWriteToSavedPhotosAlbum(self.savePhotoImage, self, @selector(photoAlbumImageSave:didFinishSavingWithError:contextInfo:), nil); } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Access is denied" message:@"Allow access to your Photos library to save this image." delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil, nil]; [alert show]; } } - (void)photoAlbumImageSave:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)context { self.savePhotoImage = nil; CGImageRelease(g_savePhotoImageRef); if (error) { NSLog(@"Error saving photo to albums: %@", error.description); } else { NSLog(@"Saved to albums!"); } } </code></pre> <p><strong>* Update *</strong> I think i've managed to narrow down my issue. I started doing trial &amp; error, where i run the app (on the device) after commenting out lines of code, to narrow things down. It looks like i may have a problem with the <strong>TakeImageBufferSnapshot</strong> function, which takes the screen buffer (using glReadPixels) and creates an CGImageRef. Now, when i try to create a UIImage out of this (using the [UIImage imageWithCGImage:] method, this seems to be why the app crashes. If I comment this line out it seems like there is no issue (other than the fact that i dont have a UIImage reference).</p> <p>I basically need a valid UIImage reference so that i can save it to the photo library (which seems to work just fine using test images).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload