I have UIWebView which has loaded an image from the internet. I have a button which opens a UIActionSheet to give you the option to tweet this picture in the UIWebView. If you hit Tweet in the UIActionSheet it disappears and you see the UIWebview again. In the background the image is loading from the internet to attach it to the tweet. This may take several time depending of the imagesize. I want to display now an information for the user that he knows what is going. I want to display a MBProgressHUD while the user is waiting that the Twitterconsole appears. I try to start the HUD when the button is press and the UIActionSheet disappears but it didn't came up. It comes up in background when the Twitterconsole appears. This a little to late. So what in the best place to start/stop the HUD? Thanks
- (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0)
{
[self hudTweet];
[self tweet];
}
- (void) tweet {
if ([TWTweetComposeViewController canSendTweet])
{
TWTweetComposeViewController *tweetSheet =
[[TWTweetComposeViewController alloc] init];
[tweetSheet setInitialText:
@"Tweeting from "];
NSString *fullURL = beverageViewString;
NSURL *url = [NSURL URLWithString:fullURL];
NSString *paths = NSTemporaryDirectory();
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:paths])
[[NSFileManager defaultManager] createDirectoryAtPath:paths withIntermediateDirectories:NO attributes:nil error:&error];
NSString *filePath = [paths stringByAppendingPathComponent:[self.title stringByAppendingFormat:@".jpeg"]];
NSData *jpegFile = [NSData dataWithContentsOfURL:url];
[jpegFile writeToFile:filePath atomically:YES];
[tweetSheet addImage:[UIImage imageWithContentsOfFile:filePath]];
[self presentModalViewController:tweetSheet animated:YES];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Sorry"
message:@"You can't send a tweet right now because your account isn't configured"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
- (void) hudTweet {
HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
[self.view.window addSubview:HUD];
HUD.delegate = self;
HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.labelText = @"Loading";
HUD.detailsLabelText = @"updating data";
}
You are downloading your image in the main thread synchronously and thereby preventing the UI from updating. See this answer for a detailed explanation and possible workarounds.
The best option for you though, is to download the image asynchronously using something like NSURLRequest (see the showURL: example and according delegate callbacks).