Question

I am using this api to share an image , but it crashes many many times !!! my code is runs fine but sometimes MBProgressHUD causes app crashing , am I using this API right ?

- (void)shareOther {

    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];

    HUD.delegate = self;
    HUD.labelText = @"Loading";

    [HUD showWhileExecuting:@selector(capture) onTarget:self withObject:nil animated:YES];


}

- (void)capture {


    //capture view
    UIGraphicsBeginImageContextWithOptions(Sview.bounds.size, Sview.opaque, 0.0);
    [Sview.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSString *str = [NSString stringWithFormat:@"some string"];
    UIActivityViewController* activityViewController =
    [[UIActivityViewController alloc] initWithActivityItems:@[screenshot , str ]
                                      applicationActivities:nil];

    UIViewController *vc = self.view.window.rootViewController;
    [vc presentViewController: activityViewController animated: YES completion:nil];

}


- (void)hudWasHidden:(MBProgressHUD *)hud {
    // Remove HUD from screen when the HUD was hidded
    [HUD removeFromSuperview];
    HUD = nil;
}
Was it helpful?

Solution

This is how I use it:

[MBProgressHUD showHUDAddedTo: self.view animated: YES];
[self doVeryLongTask];
[MBProgressHUD hideHUDForView: self.view animated: YES];

OTHER TIPS

Try to present this after removing HUD.

- (void)hudWasHidden:(MBProgressHUD *)hud {
  // Remove HUD from screen when the HUD was hidded
  [HUD removeFromSuperview];
  HUD = nil;

  UIViewController *vc = self.view.window.rootViewController;
  [vc presentViewController: activityViewController animated: YES completion:nil]; 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top