我正在开发一部分iPhone应用程序,您可以使用iPhone 3.0中的应用程序内邮件发送图像。从相机胶卷中选择图像效果很好,但是当我尝试从相机转到电子邮件时(即 - 从UIImagePickerController到MFMailComposeViewController),应用程序崩溃了。
这是运行相机的代码:
- (BOOL)startCameraPickerFromViewController:(UIViewController*)controller usingDelegate:(id<UIImagePickerControllerDelegate, UINavigationControllerDelegate>)delegateObject
{
if ( (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) || (delegateObject == nil) || (controller == nil))
return NO;
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = delegateObject;
picker.allowsImageEditing = NO;
[controller presentModalViewController:picker animated:YES];
return YES;
}
这是用相机完成的代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
NSLog(@"Called finish picking");
self.imageForSending = theImage;
// NSData *imageData = UIImageJPEGRepresentation(image, 1);
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[(ChannelTwoAppDelegate *) [[UIApplication sharedApplication] delegate] recoverNavigationBar];
[self performSelector:@selector(sendEmail) withObject:nil afterDelay:0.45];
[picker release];
}
这是发送邮件的代码:
- (void) sendEmail {
[(ChannelTwoAppDelegate *) [[UIApplication sharedApplication] delegate] hideNavigationBar];
if (![MFMailComposeViewController canSendMail])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"שגיאה", @"") message:NSLocalizedString(@"לא ניתן לשלוח מייל ממכשיר זה", @"")
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
else
{
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[[controller navigationBar] setTintColor:[UIColor colorWithRed:120.0/255.0 green:0 blue:0 alpha:1.0]];
NSData *imageData = UIImageJPEGRepresentation(imageForSending, 1);
[controller addAttachmentData:imageData mimeType:@"image/jpg" fileName:@"storyImage.jpg"];
[controller setSubject:@""];
[controller setToRecipients:[NSArray arrayWithObject:@""]];
[self presentModalViewController:controller animated:YES];
[controller release];
}
}
我删除了电子邮件地址和主题,因为它不太相关。
崩溃发生在电子邮件的presentModalViewController中。再次 - 这个精确的代码在从相机胶卷中选择图像时非常有效...
帮助? 我已经和这个人争了一段时间,并且真的可以使用一些新的输入。 谢谢!
答案 0 :(得分:0)
大家好好看,我找到了自己问题的答案。 它是两个模态视图控制器的呈现之间的时间问题
基本上,我已经知道这是一个问题,但是,我没有等待足够长的时间。 等待足以从相机胶卷中选择图像,但不足以从相机返回。
[self performSelector:@selector(sendEmail)withObject:nil afterDelay:0.45];
更改为:
[self performSelector:@selector(sendEmail)withObject:nil afterDelay:1.0];
我还添加了一个UIActivityIndicator来指示用户正在发生的事情。
快乐的编码!