0

我有 UIWebView,它已经从互联网上加载了一张图片。我有一个打开 UIActionSheet 的按钮,让您可以选择在 UIWebView 中发送这张图片。如果您在 UIActionSheet 中点击 Tweet,它会消失并且您会再次看到 UIWebview。在后台,图片正在从互联网加载以将其附加到推文中。这可能需要一些时间,具体取决于图像大小。我现在想向用户显示他知道发生了什么的信息。我想在用户等待 Twitter 控制台出现时显示 MBProgressHUD。当按下按钮并且 UIActionSheet 消失但它没有出现时,我尝试启动 HUD。当 Twitter 控制台出现时,它会在后台出现。这有点晚了。那么启动/停止 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";

}

4

1 回答 1

0

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

不过,对您来说最好的选择是使用类似 NSURLRequest 的方式异步下载图像(请参阅showURL: 示例和相应的委托回调)。

于 2012-04-21T21:05:35.770 回答