【问题标题】:Retrieve saved image from app and email从应用程序和电子邮件中检索保存的图像
【发布时间】:2011-05-17 01:05:22
【问题描述】:

用户拍照后,我成功地将图像保存到我的应用程序中。我稍后要做的是,当用户返回应用程序时,我希望他们能够将照片作为附件通过电子邮件发送。我没有任何运气将应用程序中的数据转换为图像,因此我可以将其添加为附件。有人可以指出我正确的方向吗?这是他们拍照后我保存图像的地方。

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{


    //here is the image returned

        app.aImage2 = [info objectForKey:UIImagePickerControllerOriginalImage];
        NSData *imageData = UIImagePNGRepresentation( app.aImage2 );
        NSString * savedImageName = [NSString stringWithFormat:@"r%@aImage2.png",app.reportNumber];
        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString * documentsDirectory = [paths objectAtIndex:0];
        NSString * dataFilePath;
        dataFilePath = [documentsDirectory stringByAppendingPathComponent:savedImageName];
        [imageData writeToFile:dataFilePath atomically:YES];



    [self dismissModalViewControllerAnimated:YES];


}

这里是我需要附加它的地方。

//this is inside my method that creates an email composer view
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self; // <- very important step if you want feedbacks on what the user did with your email sheet

    //how would i attach the saved image from above?

【问题讨论】:

    标签: iphone ios4 iphone-sdk-3.0


    【解决方案1】:

    这包括 Mike 在此处提到的代码:

    How to add a UIImage in MailComposer Sheet of MFMailComposeViewController

    此外,其他部分来自 Sagar Kothari 的回答:

    Sending out HTML email with IMG tag from an iPhone App using MFMailComposeViewController class

    - (void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        // Dismiss image picker modal.
        [picker dismissModalViewControllerAnimated:YES];
    
        if ([MFMailComposeViewController canSendMail]) {
            // Create a string with HTML formatting for the email body.
            NSMutableString *emailBody = [[NSMutableString alloc] initWithString:@"<html><body>"];
    
            // Add some text to it.
            [emailBody appendString:@"<p>Body text goes here.</p>"];
    
            // You could repeat here with more text or images, otherwise
            // close the HTML formatting.
            [emailBody appendString:@"</body></html>"];
            NSLog(@"%@", emailBody);
    
            // Create the mail composer window.
            MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
            emailDialog.mailComposeDelegate = self;
    
            // Image to insert.
            UIImage *emailImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    
            if (emailImage != nil) {
                NSData *data = UIImagePNGRepresentation(emailImage);
                [emailDialog addAttachmentData:data mimeType:@"image/png" fileName:@"filename_goes_here.png"];
            }
    
            [emailDialog setSubject:@"Subject goes here."];
            [emailDialog setMessageBody:emailBody isHTML:YES];
    
            [self presentModalViewController:emailDialog animated:YES];
            [emailDialog release];
            [emailBody release];
        }
    }
    

    【讨论】:

    • 还没有机会测试此代码,但这应该会引导您朝着正确的方向前进。不太确定您的“app.aImage2”是什么(我假设它是您在某处声明的 UIImage ivar)。
    • 是的,抱歉忘记在我的应用程序委托中添加“app.aImage2”是一个全局 UIImage。这行得通!很明显,我必须对其进行一些编辑,但谢谢!
    • 这段代码的大部分似乎是基于 Mike 在此处提供的代码:stackoverflow.com/questions/1527351/…,因此最好给出适当的引用。
    • 另外,其他部分来自 Sagar Kothari 的回答:stackoverflow.com/questions/3289195/…
    猜你喜欢
    • 1970-01-01
    • 2011-01-03
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多