0

在我的应用程序中,我想: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 数据作为电子邮件附件附加。

任何帮助表示赞赏。

4

2 回答 2

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 获取名称,因为我不相信它们被命名为任何有意义的东西。有关代表的任何问题或进一步的解释,请告诉我。

于 2012-04-21T01:54:07.923 回答
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];
}

这可以帮助你吗

于 2012-04-21T09:56:25.450 回答