通过电子邮件发送用户从UIpickerController中挑选的图像

时间:2011-02-18 12:40:56

标签: iphone ios4 uiimagepickercontroller

我想在我的应用中想要的是让用户在他们的库中选择一个图像,然后将该图像附加到电子邮件并发送它。

我已阅读了多个示例,但似乎无法使用此

- (IBAction)buttonTouched:(id)sender;

{     //创建图像选择器控制器     UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];

// Set source to the camera
imagePicker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;

// Delegate is self
imagePicker.delegate = self;

// Allow editing of image ?

// imagePicker.allowsImageEditing = NO;

// Show image picker
[self presentModalViewController:imagePicker animated:YES]; 

}

这是我尝试按下按钮获取图像的代码。但我如何将其纳入电子邮件,我只是不明白这是如何返回图像以及采用何种格式

非常欢迎任何帮助:)

1 个答案:

答案 0 :(得分:1)

几个星期前我有相同的请求,你必须显示UIImagePickerController并让用户选择一个图像,并将其保存在一个变量中。

UIImage *image;

接下来实施方法:

// Displays an email composition interface inside the application. Populates all the Mail fields. 
-(void)displayComposerSheet 
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    [picker setSubject:@"This is my subject"];


    // Set up recipients
    NSArray *toRecipients = [NSArray arrayWithObject:@"personOne@example.com"]; 

    [picker setToRecipients:toRecipients];

    // Attach an image to the email
    NSData *myData = UIImagePNGRepresentation(self.image);
    [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"myImage"];

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