在目录中查找照片! (编程)

时间:2011-05-11 20:36:20

标签: iphone objective-c xcode ipod-touch

来自stackoverflow的Holy Dudes,

我的问题:我需要发送一封包含3个附件的电子邮件。第一个是.txt,进展顺利,没有问题,但问题出在图片上。我用这个捕获了一个版画屏幕:

-(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

}

但是现在我需要获得我创建的这个图像!我尝试使用与文本相同的代码:

-(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];

}

这就是我的问题。我需要找到一种方法来获取我拍摄的印刷品并附上它。 因为我需要在点击“发送邮件”按钮后立即在电子邮件中提供所有附件。

感谢。

2 个答案:

答案 0 :(得分:2)

看看here如何使用base-64将图像直接嵌入到电子邮件中,这应该比将临时图像转储到用户保存的相册更清晰。

编辑: Category用于处理带有NSData的base 64,使用下载的下载。

答案 1 :(得分:2)

您应该保存到文档目录或tmp目录,而不是保存到您的照片库,以获取文档目录,您可以使用以下内容。

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

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