如何使用MFMailComposeViewController发送图像

时间:2012-02-14 14:36:52

标签: ios uiimageview mfmailcomposeviewcontroller

我正在尝试使用MFMailComposeViewController发送电子邮件,但不知何故它无法正常工作。当我不发送图像时,一切正常。但是,如果我尝试添加照片,则会出现以下错误:

-(IBAction)gonder:(id)sender{
  MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
  mailComposeViewController.mailComposeDelegate = self;

  [mailComposeViewController setSubject:@"deneme"];
  UIImage *savedImage = self.image;
  NSData *imageData = UIImagePNGRepresentation(savedImage);
  [mailComposeViewController addAttachmentData:imageData mimeType:@"image/png" fileName:@"photo"];
  NSString *emailBody = @"my text";
  [mailComposeViewController setMessageBody:emailBody isHTML:NO];

  [emailBody release];
  NSArray *alicilar=[[NSArray alloc]initWithObjects:@"mymailadress.com", nil];
  [mailComposeViewController setToRecipients:alicilar];

  [mailComposeViewController view];
}

- (void) mailComposeController:(MFMailComposeViewController*)mailComposeViewController bodyFinishedLoadingWithResult:(NSInteger)result error:(NSError*)error
{
  @try
  {
    id mailComposeController = [mailComposeViewController valueForKeyPath:@"internal.mailComposeController"];
    id sendButtonItem = [mailComposeViewController valueForKeyPath:@"internal.mailComposeView.sendButtonItem"];
    [mailComposeController performSelector:@selector(send:) withObject:sendButtonItem];
  }
  @catch (NSException *e) {}
  [mailComposeViewController release];
}

这些是错误:
2012-02-14 16:19:58.322 il[653:307] *** ERROR: FigCreateCGImageFromJPEG returned -12905. Input (null) was 444585 bytes.

2 个答案:

答案 0 :(得分:1)

您尝试附加的图像是否非常大?这可能是一个内存不足的问题。

答案 1 :(得分:1)

我认为问题在于您创建数据对象的方式。试试这个:

NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(savedImage)];

也可以考虑使用JPEG而不是PNG,它的速度要快得多。