如何通过iphone邮件附加iphone图像

时间:2010-04-03 10:28:52

标签: iphone image mfmailcomposeviewcontroller

我是iphone开发的新手。我在视图中创建了一个按钮。点击按钮,它会从Iphone加载图片库。现在我想通过邮件附加那些选定的图像。我不知道如何在MFMailComposerView中附加图像。

我如何实现这一目标,

我的代码在这里,

-(IBAction) Pictures:(id)sender
{ 
    self.imgpicker = [[UIImagePickerController alloc] init];
    self.imgpicker.delegate = self;
    self.imgpicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:self.imgpicker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img1    editingInfo:(NSDictionary *)editInfo {
    [[picker parentViewController] dismissModalViewControllerAnimated:NO]; 
    UIView *view = [[UIView alloc] init];   (This view for displaying the images)
    imageview = [[UIImageView alloc] initWithImage:img1];
    [imageview setFrame:CGRectMake(0, 0, 320, 420)];
   [self.view addSubview:imageview];
    [view release];

  UIBarButtonItem *rightbutton = [[UIBarButtonItem alloc] initWithTitle:@"Email"   style:UIBarButtonItemStyleBordered target:self action:@selector(rightbutton)];
  self.navigationItem.rightBarButtonItem = rightbutton;
  [rightbutton release];

   }

  -(void) rightbutton
  {
      [self emailImage:(UIImage *)image];( how to pass the image to mail view)
  }

  - (void)emailImage:(UIImage *)image
  {
      picker = [[MFMailComposeViewController alloc] init];
      picker.mailComposeDelegate = self;
      [picker setToRecipients:[NSArray arrayWithObjects:@"aaaa@nnn.com",nil]];
      NSData *data = UIImagePNGRepresentation(image);
      [picker addAttachmentData:data mimeType:@"image/png" fileName:@"iPod Library Image"]; 
      [self presentModalViewController:picker animated:YES];
      [picker release];
    }

请帮帮我。

感谢。

1 个答案:

答案 0 :(得分:1)

您可能应该在控制器类(imageThatWasPicked等)中设置UIImage实例变量,然后在从-imagePickerController:didFinishPickingImage:editingInfo:获取图像时设置该实例变量。然后,您可以在调用-emailImage:时引用此实例变量。

注意,您正在泄漏imageView,我不知道您在图像选择器委托方法中尝试使用view做什么。您可以完全摆脱分配和释放view的代码。