iOS-将相机选中的图像附加到电子邮件奇怪的行为

时间:2012-12-28 12:13:37

标签: uiimagepickercontroller mfmailcomposeviewcontroller

我创建了一个示例应用来测试如何将相机挑选的图像作为电子邮件的附件发送。 在那我有2buttons。一个(相机按钮)打开相机&另一个按钮(电子邮件按钮)打开MFMailComposeViewController

imageAttachment在.h文件中声明为UIImage类型

#pragma mark - Picker Controller delegate
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    imageAttachment = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

//    [self performSelector:@selector(dismissCamera) withObject:nil afterDelay:2.0];

    [self performSelector:@selector(emailImage) withObject:nil afterDelay:1.0];
    [self dismissModalViewControllerAnimated:YES];

    [picker release];
}

在上面的方法中我遇到了问题。如果我取消注释该行

//    [self performSelector:@selector(dismissCamera) withObject:nil afterDelay:2.0];

&安培;评论以下行

[self performSelector:@selector(emailImage:) withObject:image afterDelay:1.0];
[self dismissModalViewControllerAnimated:YES];

稍后我触摸电子邮件按钮然后崩溃了。 这很奇怪..为什么它会崩溃?它崩溃没有任何错误。?

这些是其他方法

- (void)emailImage
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"];
    [picker setToRecipients:toRecipients];

    [picker setSubject:@"Photo"];

    NSString *emailBody = @"Photo clicked ;
    [picker setMessageBody:emailBody isHTML:NO];

    // Create NSData object as PNG image data from camera image
    NSData *myData = UIImagePNGRepresentation(imageAttachment);

    // Attach image data to the email
    // 'CameraImage.png' is the file name that will be attached to the email
    [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"CameraImage.png"];

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

以下是崩溃应用程序的代码。

- (void)dismissCamera
{
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)btnEmailTouched:(id)sender
{    
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil)
    {
        // We must always check whether the current device is configured for sending emails
        if ([mailClass canSendMail])
            [self displayComposerSheet];
        else
            [self launchMailAppOnDevice];
    }
    else
        [self launchMailAppOnDevice];
}

#pragma mark Compose Mail
// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayComposerSheet
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    [picker setSubject:@"Photo"];

    [picker setToRecipients:[NSArray arrayWithObjects:@"emailaddress1@domainName.com", nil]];

    NSString *emailBody = @"Photo clicked;
    [picker setMessageBody:emailBody isHTML:NO];

    // Create NSData object as PNG image data from camera image
    NSData *data = UIImagePNGRepresentation(imageAttachment);

    // Attach image data to the email
    // 'CameraImage.png' is the file name that will be attached to the email
    [picker addAttachmentData:data mimeType:@"image/png" fileName:@"CameraImage.png"];

    [self presentModalViewController:picker animated:YES];

    [picker release];
}

PS:两种方法- (void)emailImage& -(void)displayComposerSheet使用相同的行代码。我附加了相同的 imageAttachemnt 对象作为电子邮件附件,但是当我关闭相机时它失败了。触摸电子邮件按钮以打开MFMailComposeViewController

1 个答案:

答案 0 :(得分:0)

为什么要在方法

中发布选择器
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info