簡體   English   中英

如何在iPhone圖庫中的郵件中附加圖像?

[英]How to attach image in mail from iPhone gallery?

我可以從iPhone畫廊中檢索圖像,並在imageView中顯示它,但我無法將其附加在郵件中...它只是顯示一個? 郵件中的圖片...。有人可以幫助我嗎...提前謝謝

http://imageshack.us/photo/my-images/826/screenshot20120525at124.png/

這是我在發送郵件時收到的wat的屏幕截圖。

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self; 
[picker setSubject:@"My Image Is Attached"];

// other mail settings here

//Attachment imageData1 is NSData of your image.
[picker addAttachmentData:imageData1 mimeType:@"image/png" fileName:@"foo.png"]

請嘗試以下代碼:

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@"Hello"];


// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"]; 
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil]; 
NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"]; 

[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];  
[picker setBccRecipients:bccRecipients];

// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"];

// Fill out the email body text
NSString *emailBody = @"It is raining";
[picker setMessageBody:emailBody isHTML:NO];

[self presentModalViewController:picker animated:YES];
[picker release];

您應該有這樣的東西:

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
[picker setSubject:....]
...

您要做的就是:

NSData *data = UIImagePNGRepresentation(image);

[picker addAttachmentData:data mimeType:@"image/png" fileName:@"TheNameYouWant"];

其中imageUIImage類型。

更新:

難道只是一個延遲的問題嗎? 您發布的屏幕截圖恰好是我在加載圖像時得到的圖像。 您是否嘗試等待幾秒鍾...?

首先,在項目中導入名為-MessageUI.framework的導入框架。

然后導入

#import <MessageUI/MFMailComposeViewController.h>     

在您的viewcontroller.h部分中。 現在添加委托

<MFMailComposeViewControllerDelegate>

現在,您可以使用Mail composer視圖。

-(IBAction)showMail:(id)sender //this is your UIButton action
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self; 
    [picker setSubject:@"write your subject here"];

    UIImage *image = [UIImage imageNamed:@"anyImage.png"]; 

    //convert UIImage to NSData to add it as attachment

    NSData *imageData = UIImagePNGRepresentation(image);

    //this is how to attach any data in mail, remember mimeType will be different
    //for other data type attachment.

   [picker addAttachmentData:imageData mimeType:@"image/png" fileName:@"image.png"];

   //showing MFMailComposerView here
   [self presentModalViewController:picker animated:YES];

}

您可以在完成工作后關閉MFMailComposerView,如下所示:-

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    if(result == MFMailComposeResultCancelled)
        NSLog(@"Mail has cancelled");
    if(result == MFMailComposeResultSaved)
        NSLog(@"Mail has saved");

    [self dismissModalViewControllerAnimated:YES];
}

希望這會有所幫助。

謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM