Felipe
Felipe

Reputation: 510

Find Photo in Directory! (Programmatically)

Holy Dudes from stackoverflow,

MY PROBLEM: I need to send an email with 3 attachments. The first that is the .txt, is going well, no problems with him, but the problem is with the pictures. I capture a printscreen with this:

-(IBAction)screenShot{
    maintimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(update) userInfo:nil repeats:YES];
}

-(void)update{
    time = time + 1;
    if(time == 2){
        [self hideThem]; //hides the buttons
        UIGraphicsBeginImageContext(self.bounds.size);
        [self.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        UIImageWriteToSavedPhotosAlbum(screenImage, nil, nil, nil);

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"GRAPH SAVED" message:@"YOUR GRAPH HAS BEEN SAVEN IN THE PHOTO ALBUM" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        [maintimer invalidate];
        time = 0;
    }
    [self bringThem]; //brings the buttons back

}

But now i need to get this image i created! I tried using the same code i use for the text:

-(IBAction)sendMail
{
    (..)
        //HERE IS WHERE THE NAME IS GIVEN
    NSString *filename = [NSString stringWithFormat:@"%f.txt", [[NSDate date] timeIntervalSince1970]];

        //THIS IS GOING TO STORE MY MESSAGE STUFF
    NSString *content = [[NSMutableString alloc]init];

        (...) //LOTS OF UNNECESSARY CODE

        //HERE IS WHERE IT BILDS THE FILE
    [self writeToTextFile:filename content:content];

         //HERE IS THE CODE WHERE IT ATTACHES THE FILE
    [self sendMailWithFile:filename];
    //[content release];
}

-(void)sendMailWithFile : (NSString *)file 
{
        //THIS IS THE CODE THAT GETS THE TEXT
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);   
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fileName = [NSString stringWithFormat:@"%@/%@", documentsDirectory, file]; 
    NSData *data = [[NSData alloc] initWithContentsOfFile:fileName];


    //THIS WAS MY TRY TO GET THE PHOTO ON THE SAME STYLE OF THE TEXT              
    NSArray *photoPath = NSSearchPathForDirectoriesInDomains(NSPicturesDirectory, NSUserDomainMask, YES);
    NSString *photoDirectoryForFirstPhoto = [photoPath objectAtIndex:0];
    NSString *photoName_First = [NSString stringWithFormat:@"%@/%@", photoDirectoryForFirstPhoto, /*I DO NOT HAVE THE PHOTO NAME*/@"FirstPhoto"];
    NSData *photoData_First = [[NSData alloc] initWithContentsOfFile:photoName_First];


    MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];

    if ([MFMailComposeViewController canSendMail]) 
    {
        mailComposer = [[MFMailComposeViewController alloc] init];
        mailComposer.mailComposeDelegate = self;
        [mailComposer setSubject:@"RK4 data"];

        [mailComposer addAttachmentData:data mimeType:@"text/plain" fileName:@"[RK4][EQUATION][TEXT]"];
        [mailComposer addAttachmentData:photoData_First mimeType:@"image/png" fileName:@"[RK4][EQUATION][IMAGE]"];
        [mailComposer setMessageBody:@"iCalculus Runge-Kutta data." isHTML:NO];
        [self presentModalViewController:mailComposer animated:YES];

    }
    else (...) 

    [mailComposer release];
    [data release];

}

So that's my problem. I need to find a way to get the printscreen i took and attach it. Because i need to come up with all the attaches in the email as soon as he clicks the "SEND MAIL" button.

Thanks.

Upvotes: 1

Views: 1323

Answers (2)

BarrettJ
BarrettJ

Reputation: 3431

Take a look here for how to embed the image directly inside the email using base-64, that should be much cleaner than dumping temporary images to the users saved photos album.

Edit: Category for handling base 64 with NSData, use the download towards the bottom.

Upvotes: 2

Grady Player
Grady Player

Reputation: 14549

Rather than saving to your photo library, you should save to documents directory, or tmp directory, to get documents directory you can use the following.

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* dd = [paths objectAtIndex:0];

NSString *path = [dd stringByAppendingPathComponent:@"file.png"];
[UIImagePNGRepresentation(screenImage) writeToFile:path atomically:NO];

Upvotes: 2

Related Questions