2012-04-20 69 views
0

在我的应用我想: 一)当前相机用户拍摄照片 二)创建电子邮件,并附上图片拍摄通过电子邮件发送与ImagePickerController拍摄的照片?

我想出如何拍摄照片。但是,我不知道如何将其附加到电子邮件。在我看到的例子中,文件名是已知的。这是一个例子。

picker.mailComposeDelegate = self; 
    [picker setSubject:@"I have a pencil for you"]; 

    UIImage *roboPic = [UIImage imageNamed:@"RobotWithPencil.jpg"]; 
    NSData *imageData = UIImageJPEGRepresentation(roboPic, 1); 
    [picker addAttachmentData:imageData mimeType:@"image/jpg" fileName:@"RobotWithPencil.jpg"]; 

    NSString *emailBody = @"This is a cool image of a robot I found. Check it out!"; 
    [picker setMessageBody:emailBody isHTML:YES]; 

    [self presentModalViewController:picker animated:YES]; 

我从回调方法获取图像,它确实存储在相机胶卷中。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 


    // Access the uncropped image from info dictionary 
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    [picker dismissModalViewControllerAnimated:TRUE]; 
    [picker release]; 
    } 

但是从那里,我不知道如何将图像附加到电子邮件?主要是因为当使用UIImagePickerController相机源获取照片时,我不知道当照片添加到相机胶卷时文件名是什么?

所以,我想我需要:。 一)找到一种方式来获得的图像文件的名称一旦被保存或 B)的另一种方式附加的UIImage数据作为电子邮件的附件。

任何帮助表示赞赏。

回答

0

也许我不明白你正在尝试做的UI明智的,但为什么会这样不会从您的代码工作提供:

编辑:(改代码使用委托)

- (void)imagePickerController:(UIImagePickerController *)imagepicker didFinishPickingMediaWithInfo:(NSDictionary *)info { 


// Access the uncropped image from info dictionary 
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
if([delegate respondsToSelector:@selector(didSelectImage:)]) 
    [delegate didSelectImage:image]; 
} 

这正好在你的头文件(通常高于类声明):

@protocol PostHelperDelegate <NSObject> 

@optional 
- (void)DimissModal; 
//image data for image selected 
- (void)DidSelectImage:(UIImage*)image; 
@end 

还需要在你的类来创建这个属性

@property(nonatomic,assign)id<PostHelperDelegate>* delegate; //don't forgot to synthizie 

然后在类实现委托要分配委托类:

Poster = [[delegateclass alloc] init]; 
    Poster.delegate = self; 

然后只需添加代理功能:

- (void)DidSelectImage:(UIImage*)image 
{ 
    //handle image here 
} 

我这简化了不少,让我知道如果你有任何麻烦得到它的工作

http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/

BEFORE UPDATE:

我个人会使用委托来通知图像已被接走,比启动邮件选择器,但那是另一回事。至于UIImage,我不知道一种方式或从来没有找到一种方法来命名/或从UIImagePicker获取名称,因为我不相信它们被命名为任何有意义的东西。对代表的任何问题或进一步的解释让我知道。

+0

我没有加这个片段到didFinishPickingMediaWithInfo。但是,它仍然没有按预期工作。缺少的一件事是MailPicker声明本身。所以,我补充说。但是,它从不显示。不知道为什么?我添加的代码是这样的: MFMailComposeViewController * picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; – 2012-04-21 02:57:17

+0

我认为代表建议是必要的。部分问题是处理两个模式视图控制器,并尝试关闭ImagePicker,然后从上述事件中打开MailPicker。 – 2012-04-24 03:18:32

+0

我更新了上面的代码以显示如何执行代理。我也建议先打开mailpicker视图,然后从那里打开图像选择器视图打开后。由于iOS导航的工作方式(也可能使用户随着一堆模式视图的上下移动),尝试关闭模式视图并立即打开另一个模式视图时,您总会遇到问题。 – daltoniam 2012-04-24 21:56:42

0

使用娄代码

-(void)yourmethodname_clicked{ 
     UIImagePickerController * picker = [[UIImagePickerController alloc] init]; 
       picker.delegate = self; 
     if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
       picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
       [self presentModalViewController:picker animated:YES]; 
     } 
     else{ 
       UIAlertView *altnot=[[UIAlertView alloc]initWithTitle:@"Camera Not   Available" message:@"Camera Not Available" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 

       [altnot show]; 
       [altnot release]; 

     } 
    } 


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
[picker dismissModalViewControllerAnimated:YES]; 


MFMailComposeViewController *mailpicker = [[MFMailComposeViewController alloc] init]; 

    [mailpicker.navigationBar setTintColor:[UIColor blackColor]]; 
    mailpicker.mailComposeDelegate = self; 

    [mailpicker setSubject:@"set your subject"]; 

    NSArray *toRecipients = [NSArray arrayWithObjects: nil]; 

    [mailpicker setToRecipients:toRecipients]; 

    UIImage *picture = [info objectForKey:@"UIImagePickerControllerOriginalImage"]];; 

    NSData *imageData = UIImagePNGRepresentation(picture); 
    [mailpicker addAttachmentData:imageData mimeType:@"image/png" fileName:@"set your file name"]; 

    NSString *emailBody = @"set your body string"; 
    [mailpicker setMessageBody:emailBody isHTML:YES]; 
    [mailpicker setSubject:@"set your subject"]; 
    mailpicker.title = @"Email"; 

    [self presentModalViewController:mailpicker animated:YES]; 
    [mailpicker release]; 
} 

这可以帮助你