2 lpploverou LPPloveROU 于 2012.11.02 10:29 提问

点击按钮实现email附加捕捉图片

iphone应用中,捕捉图片使用UIImagePickerController:

    - (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个回答

Mr_me
Mr_me   2012.11.02 14:32

修改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];
      }

如果已经声明了emailbutton:

UIButton *emailbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

然后添加:

[emailbutton addTarget:self action:@selector(emailButtonPressed) forControlEvents:UIControlEventTouchUpInside];
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!