0

我创建了一个示例应用程序来测试如何将相机拾取的图像作为电子邮件的附件发送。在那我有2个按钮。一个(相机按钮)打开相机和另一个按钮(电子邮件按钮)打开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

4

1 回答 1

0

你为什么要在方法中释放选择器

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
于 2012-12-28T12:17:54.887 回答