用UIImagePickerController发送电子邮件照片?

时间:2012-04-20 21:14:57

标签: iphone

在我的应用中,我想: a。)将相机呈现给用户拍照 b。)创建电子邮件并附上照片

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

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相机源获取照片时,将照片添加到相机胶卷时我不知道文件名是什么?

所以,我想我需要: a。)找到一种方法来获取图像文件的名称保存后 b。)将UIImage数据作为电子邮件附件附加的另一种方法。

任何帮助表示感谢。

2 个答案:

答案 0 :(得分: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/

更新前:

个人我会使用委托来通知图像已被选中而不是启动邮件选择器,但这是另一回事。至于UIImage,我不知道某种方式或者从未找到过从UIImagePicker中获取名称的方法,因为我不认为它们被命名为有意义的任何名称。有关代表的任何问题或进一步的解释让我知道。

答案 1 :(得分: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];
}

这可以帮到你