单击按钮将捕获的图像附加到电子邮件

时间:2012-11-01 19:28:30

标签: iphone ios uiimagepickercontroller

我使用下面的UIImagePickerController委托方法从iphone中捕获了图像。

    - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    self.myImageView.image = image;  
    //    [self performSelector:@selector(emailButtonPressed:) withObject:image afterDelay:1.0];
     [self dismissModalViewControllerAnimated:YES];  
}

请参见emailButtonPressed调用的上述self方法。我想在按钮动作中调用它。 我为emailButtonPressed编写了以下代码。

- (void)emailButtonPressed:(UIImage *)image
{  
        MFMailComposeViewController *mailview=[[MFMailComposeViewController alloc]init];      mailview.navigationBar.tintColor=[UIColor colorWithRed:55/255.0 green:190/255.0 blue:55/255.0 alpha:1];  
        mailview.mailComposeDelegate=self;  
        // NSMutableString *subject=[NSMutableString stringWithFormat:@"%@",@"Testing"];  
        [mailview setSubject:@"Picture from my iPhone!"];  
        //   NSString *email_new=@"";  
        [mailview setMessageBody:@"Description" isHTML:NO];  

        NSData *imageData = UIImagePNGRepresentation(image);  

        [mailview addAttachmentData:imageData mimeType:@"image/png" fileName:@"ImageName"];  
        [self presentModalViewController:mailview animated:YES];  
}
抱歉我的代码中有任何错误。

1 个答案:

答案 0 :(得分:0)

按如下方式修改emailButtonPressed方法,

- (void)emailButtonPressed //removed the param
{  
        UIImage *image = self.myimageview.image; //or set some other param as image = self.image; whichever you set in picker delegate method
        MFMailComposeViewController *mailview=[[MFMailComposeViewController alloc]init];      mailview.navigationBar.tintColor=[UIColor colorWithRed:55/255.0 green:190/255.0 blue:55/255.0 alpha:1];  
        mailview.mailComposeDelegate=self;  
        // NSMutableString *subject=[NSMutableString stringWithFormat:@"%@",@"Testing"];  
        [mailview setSubject:@"Picture from my iPhone!"];  
        //   NSString *email_new=@"";  
        [mailview setMessageBody:@"Description" isHTML:NO];  

        NSData *imageData = UIImagePNGRepresentation(image);  

        [mailview addAttachmentData:imageData mimeType:@"image/png" fileName:@"ImageName"];  
        [self presentModalViewController:mailview animated:YES];  
}

保持此方法不变,

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    self.myImageView.image = image;  //instead of this, you can create an @property for image in .h file and assign to that also here.
    [self dismissModalViewControllerAnimated:YES];  
}

假设您已将电子邮件按钮声明为

UIButton *emailbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //or any other way

现在,在该行之后立即将此方法添加为按钮目标

[emailbutton addTarget:self action:@selector(emailButtonPressed) forControlEvents:UIControlEventTouchUpInside];

现在点击emailbutton self.myimageview.image代理中UIImagePickerController所设置的任何图片都将作为附件发送。