问题描述
- 点击按钮实现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]; }
请多指教,谢谢~
解决方案
修改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];
}
- (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];
时间: 2024-09-08 11:15:05