0

How can I code an in-app screenshot that can then be attached to a text without having to save it to the photo library? I have read the 'in-app Screenshot and attach to email without saving into library' this was helpful, but still not what I need. I need this to appear in the text message box. Thanks!

2 Answers2

1

Here is a how to do an in app screenshot from Apple website:

- (UIImage*)snapshot:(UIView*)eaglview
{
    GLint backingWidth, backingHeight;

    // Bind the color renderbuffer used to render the OpenGL ES view
    // If your application only creates a single color renderbuffer which is already bound at this point, 
    // this call is redundant, but it is needed if you're dealing with multiple renderbuffers.
    // Note, replace "_colorRenderbuffer" with the actual name of the renderbuffer object defined in your class.
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, _colorRenderbuffer);

    // Get the size of the backing CAEAGLLayer
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);

    NSInteger x = 0, y = 0, width = backingWidth, height = backingHeight;
    NSInteger dataLength = width * height * 4;
    GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte));

    // Read pixel data from the framebuffer
    glPixelStorei(GL_PACK_ALIGNMENT, 4);
    glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);

    // Create a CGImage with the pixel data
    // If your OpenGL ES content is opaque, use kCGImageAlphaNoneSkipLast to ignore the alpha channel
    // otherwise, use kCGImageAlphaPremultipliedLast
    CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, data, dataLength, NULL);
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGImageRef iref = CGImageCreate(width, height, 8, 32, width * 4, colorspace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast,
                                    ref, NULL, true, kCGRenderingIntentDefault);

    // OpenGL ES measures data in PIXELS
    // Create a graphics context with the target size measured in POINTS
    NSInteger widthInPoints, heightInPoints;
    if (NULL != UIGraphicsBeginImageContextWithOptions) {
        // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
        // Set the scale parameter to your OpenGL ES view's contentScaleFactor
        // so that you get a high-resolution snapshot when its value is greater than 1.0
        CGFloat scale = eaglview.contentScaleFactor;
        widthInPoints = width / scale;
        heightInPoints = height / scale;
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(widthInPoints, heightInPoints), NO, scale);
    }
    else {
        // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
        widthInPoints = width;
        heightInPoints = height;
        UIGraphicsBeginImageContext(CGSizeMake(widthInPoints, heightInPoints));
    }

    CGContextRef cgcontext = UIGraphicsGetCurrentContext();

    // UIKit coordinate system is upside down to GL/Quartz coordinate system
    // Flip the CGImage by rendering it to the flipped bitmap context
    // The size of the destination area is measured in POINTS
    CGContextSetBlendMode(cgcontext, kCGBlendModeCopy);
    CGContextDrawImage(cgcontext, CGRectMake(0.0, 0.0, widthInPoints, heightInPoints), iref);

    // Retrieve the UIImage from the current context
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    // Clean up
    free(data);
    CFRelease(ref);
    CFRelease(colorspace);
    CGImageRelease(iref);

    return image;
}
nickfox
  • 2,835
  • 1
  • 26
  • 31
  • Hi nickfox, thanks for the response. This looks helpful, although it looks like an attachment for an email, rather then a text/mms. Can you confirm that the link is for MMS and not email? Thanks – user1475972 Jun 23 '12 at 07:43
0

I have looked around the internet, and the documentation, and unlike the email version, MFMessageComposeViewController has no 'attachment' property, so you can't send any. This is also further confirmed in this other SO post - How can my app send MMS with a photo?.

Jonathan

Community
  • 1
  • 1
Jonathan King
  • 1,528
  • 14
  • 25
  • Hi Jonathan, thanks for the message. I do hope we can send our attachments via MMS. There must be someway of being able to add code into the body of a text. If Apple can code it to do it in the first place, then I'm sure we can code something to do the same. Smileys for example, they manage to go into a text, as well as pictures from the photo library. – user1475972 Jun 23 '12 at 07:48
  • @nickfox that answer was regarding emails, not messages, which I said was possible in my answer. – Jonathan King Jun 23 '12 at 08:33
  • @user1475972 if by saying smileys you mean emjoi's, then they are not images, but font glyphs. Also do you have an example of a third party app that can send photo's as attachments? - As I have never seen one. A possible solution however would be to upload the photo's to a online service from the app, and send a link to it in the SMS, which would also be good for the customer as they wouldn't have to pay extra for a MMS. – Jonathan King Jun 23 '12 at 08:37
  • nice one thanks Jonathan. No example, and thanks for the education on the emjoi's. I didn;t realise they wasn't images, and find glyphs would make sense. Again thanks for the help : ) – user1475972 Jun 23 '12 at 17:19
  • Glad to have helped! Any chance you could mark my answer as the 'correct answer', as it is, and it means we both get points ;) – Jonathan King Jun 23 '12 at 21:39
  • I found an app that send a picture via mms. It's called BirthDay cards free – user1475972 Jun 27 '12 at 14:11
  • If you use it you will find that it copies the image to the pasteboard and then tells you to paste it into a message. – Jonathan King Jun 27 '12 at 15:44
  • nice one thanks jonathan, did that manage to solve what you needed? or did you have to think up something else? – user1475972 Jun 27 '12 at 17:59