Thursday 18 April 2013

Snapshot a View and Attach the image into Mail for Xcode

Add the Frameworks :   MessageUI.framework & QuartzCore.framework

.h File:
#import <QuartzCore/QuartzCore.h>
#import <MessageUI/MessageUI.h>

.m File:

-(void)Button
{
    
    myScrollView.backgroundColor= [UIColor colorWithPatternImage:[UIImage imageNamed:@"background123.png"]];
    
    UIImage *image;
    UIGraphicsBeginImageContext(myScrollView.contentSize);
    {
        CGPoint savedContentOffset = myScrollView.contentOffset;
        CGRect savedFrame = myScrollView.frame;
        myScrollView.contentOffset = CGPointZero;
        myScrollView.frame = CGRectMake(00myScrollView.contentSize.width,  myScrollView.contentSize.height);
        
        [myScrollView.layer renderInContextUIGraphicsGetCurrentContext()];
        image = UIGraphicsGetImageFromCurrentImageContext();
        
        myScrollView.contentOffset = savedContentOffset;
        myScrollView.frame = savedFrame;
    }
    
    UIGraphicsEndImageContext();
   //Save Image to Local Library
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
    //Attach a Email Process
    NSData * imageData = UIImageJPEGRepresentation(image, 1.0);
    
    
    if ( [MFMailComposeViewController canSendMail] ) 
    {
        MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController allocinitautorelease];
       mailComposer.mailComposeDelegate = self;
        [mailComposer addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"SeatChart.jpg"];
        [mailComposer setSubject:@"Seating Chart"];
       
        
        /* Configure other settings */
        
        [self presentModalViewController:mailComposer animated:YES];
        //[mailComposer release];
    }
    else
    {
        UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@"No Mail Accounts" message:@"Please set up a Mail account in order to send email." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }   
}


- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    UIAlertView *alert;
    if (error)
    {
        alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                           message:@"Unable to save image to Photo Album."
                                          delegate:self cancelButtonTitle:@"Ok"
                                 otherButtonTitles:nil];
    }
    else
    {
        alert = [[UIAlertView alloc] initWithTitle:@"Success"
                                           message:@"Image saved to Photo Album."
                                          delegate:self cancelButtonTitle:@"Ok"
                                 otherButtonTitles:nil];
    }
    [alert show];

}


- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    UIAlertView *Alert;
    switch (result)
    {

        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            Alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"E-Mail Cancelled" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nilnil];
            [Alert show];
            break;

        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            Alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"E-Mail Saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nilnil];
            [Alert show];
            break;

        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            Alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"E-Mail Sent" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nilnil];
            [Alert show];
            break;

        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            Alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"E-Mail Sending Failed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nilnil];
            [Alert show];
            break;

        default:
            break;
    }
    
    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];

}

Snapshot a View and Attach the image into Mail (View Only) for Xcode

Add the Frameworks :   MessageUI.framework & QuartzCore.framework

.h File:
#import <QuartzCore/QuartzCore.h>
#import <MessageUI/MessageUI.h>

.m File:


-(void)Button
{
    
    myScrollView.backgroundColor= [UIColor colorWithPatternImage:[UIImage imageNamed:@"background123.png"]];
    
    UIGraphicsBeginImageContext(self.view.frame.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    
    //Save Image to Local Library
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

    

    //Attach a Email Process
    UIImageWriteToSavedPhotosAlbum(image, nilnilnil);
    
    NSData * imageData = UIImageJPEGRepresentation(image, 1.0);
    
    
    if ( [MFMailComposeViewController canSendMail] ) 
    {
        MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController allocinitautorelease];
       mailComposer.mailComposeDelegate = self;
        [mailComposer addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"SeatChart.jpg"];
        [mailComposer setSubject:@"Seating Chart"];
       
        
        /* Configure other settings */
        
        [self presentModalViewController:mailComposer animated:YES];
        //[mailComposer release];
    }   
}


- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    UIAlertView *alert;
    if (error)
    {
        alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                           message:@"Unable to save image to Photo Album."
                                          delegate:self cancelButtonTitle:@"Ok"
                                 otherButtonTitles:nil];
    }
    else
    {
        alert = [[UIAlertView alloc] initWithTitle:@"Success"
                                           message:@"Image saved to Photo Album."
                                          delegate:self cancelButtonTitle:@"Ok"
                                 otherButtonTitles:nil];
    }
    [alert show];

}


- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    UIAlertView *Alert;
    switch (result)
    {

        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            Alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"E-Mail Cancelled" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [Alert show];
            break;

        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            Alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"E-Mail Saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [Alert show];
            break;

        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            Alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"E-Mail Sent" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [Alert show];
            break;

        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            Alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"E-Mail Sending Failed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [Alert show];
            break;

        default:
            break;
    }
    
    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];

}

No comments: