MBProgressHUD显示Twitter

时间:2012-03-15 19:00:03

标签: iphone ios twitter uiwebview mbprogresshud

我有UIWebView,它已从互联网上加载图像。我有一个按钮,打开一个UIActionSheet,让您可以选择在UIWebView中发布这张图片。如果您在UIActionSheet中点击Tweet,它就会消失,您再次看到UIWebview。在后台,图像从互联网加载,以将其附加到推文。根据图像尺寸,这可能需要几个时间。 我想现在向用户显示他知道将要发生什么的信息。我想在用户等待Twitterconsole出现时显示MBProgressHUD。 按下按钮时,我尝试启动HUD,UIActionSheet消失,但没有出现。当Twitterconsole出现时,它会出现在后台。这有点晚了。 那么开始/停止HUD的最佳位置是什么? 感谢

- (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";

}

1 个答案:

答案 0 :(得分:0)

您正在同步下载主线程中的图像,从而阻止UI更新。有关详细说明和可能的解决方法,请参阅this answer

对您而言,最好的选择是使用NSURLRequest等异步下载图像(请参阅showURL: example并根据delegate callbacks)。