カメラで撮影した画像をメールの添付ファイルとして送信する方法をテストするサンプル アプリを作成しました。その中で私は2ボタンを持っています。カメラを開く1 つ (カメラ ボタン) と、カメラを開く別のボタン (メール ボタン)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