在我绘制图表的应用程序中,可以选择将图表作为图像发送并将图表保存到照片库。但是图表可能包含多达 4000 个或更多的数据,因此获取图像需要时间。因此,我在一个单独的线程上进行这项工作,并使用进度 hud 来指示活动,如下所示:
- (void)sendImageAsMail
{
if ([MFMailComposeViewController canSendMail])
{
// Configure HUD
self.myHUD.mode = MBProgressHUDModeIndeterminate;
self.myHUD.labelText = @"Acquiring image, please wait";
self.myHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
__block UIImage *graphImage;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
CPTGraph *currentGraph = self.hostView.hostedGraph;
graphImage = [currentGraph imageOfLayer];
self.imageData = UIImagePNGRepresentation(graphImage);
[[NSNotificationCenter defaultCenter] postNotificationName:@"mailReady" object:nil];
});
}
else
{
[self showAlertWithTitle:@"Cannot Send Mail" andText:@"Your mail configuration is incorrect"];
}
}
- (void)mailReady:(NSNotification *)notification
{
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
mailVC.mailComposeDelegate = self;
[mailVC setSubject:[NSString stringWithFormat:@"some subject here (%@)", [[[iDSConstants sharedConstants]codeTypes] objectAtIndex:_codeIndex]]];
[mailVC setMessageBody:@"some body" isHTML:NO];
[mailVC addAttachmentData:self.imageData mimeType:@"image/png" fileName:@"Design Spectrum"];
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
[self presentViewController:mailVC animated:YES completion:nil];
}
现在有趣的是,在显示 mailviewcontroller 之前,hud 没有被解除(或隐藏,如果可能的话);如果我按下取消并关闭电子邮件,我可以看到平视显示器在旋转,并且它会继续旋转几秒钟。我尝试了其他方法,例如在代码的不同位置关闭 hud:
(上面代码的一部分)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
CPTGraph *currentGraph = self.hostView.hostedGraph;
graphImage = [currentGraph imageOfLayer];
self.imageData = UIImagePNGRepresentation(graphImage);
**// get main thread for dismissing the hud
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});**
[[NSNotificationCenter defaultCenter] postNotificationName:@"mailReady" object:nil];
});
但我仍然可以看到之前应该隐藏的平视显示器。对此问题的任何见解将不胜感激。