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

发布于 2024-11-06 19:01:56 字数 3435 浏览 0 评论 0原文

来自 stackoverflow 的神圣家伙,

我的问题:我需要发送一封包含 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];

}

所以这就是我的问题。我需要找到一种方法来获取我拍摄的打印屏幕并将其附加起来。 因为我需要在他单击“发送邮件”按钮后立即拿出电子邮件中的所有附件。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Smile简单爱 2024-11-13 19:01:56

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

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

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

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];
歌入人心 2024-11-13 19:01:56

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文