UIAlertView与UIActivityIndi​​cator相矛盾

时间:2012-03-20 14:57:00

标签: iphone ios uialertview uiactivityindicatorview

离线时,我正在从在线图像下载图像以进行缓存。当我打开应用程序时,图像在UIAlertView弹出之前完全下载。它完成不正确。我想在下载图像之前制作UIAlertView pop。这里有我的代码。

- (void) operatePopupUpdating {
    myAlert = [[UIAlertView alloc] initWithTitle:@"Updating database.." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [myAlert show];

    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    indicator.center = CGPointMake(myAlert.bounds.size.width / 2, myAlert.bounds.size.height - 50);
    [indicator startAnimating];
    [myAlert addSubview:indicator];

    [self operateUpdate];
    [self updateImage];

    [self operationCompleted];
}

在updateImage,operateUpdate和operationCompleted Method

- (void)updateImage {
NSString *snake_ico_file = @"snake_img_icn_";
NSString *filePath = [self dataFile:[snake_ico_file stringByAppendingString:@"0.jpg"]];
int max_count = [[self readData] count];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
    for (int i = 0; i < max_count; i++) {
        NSString *path = @"http://exitosus.no.de/drngoo/image/";
        path = [path stringByAppendingFormat:@"%d",i];
        NSString *ico_path = [path stringByAppendingFormat:@"/ico"];

        //icon            
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *filename = [snake_ico_file stringByAppendingFormat:@"%d.jpg"];
        NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:filename];
        NSData *thedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:ico_path]];;
        [thedata writeToFile:localFilePath atomically:YES];
       }
}

-(void) operationCompleted
{
       [myAlert dismissWithClickedButtonIndex:0 animated:YES];

       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message form System" message:@"Database was updated successful" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

       [alert show];
}

- (void)operateUpdate {

     NSString *filePath = [self dataFileSettingPath];
     int updatedVer = 0;
     int version = [self checkDatabase];
     if (version == -1) {
         updatedVer = [self createDataBase:0];
     } else {
         updatedVer = [self updateDataBase:version];
     }

     [self.settingInfo setObject:[NSString stringWithFormat:@"%d",updatedVer] forKey:@"DBVersion"];
   [self.settingInfo writeToFile:filePath atomically:YES];
}

我如何修复它们并正常工作?

1 个答案:

答案 0 :(得分:1)

只有在您的图片下载完成后才会显示alertView。解决此问题的一个解决方案是,

创建另一个类ImageDownLoad.h

in .h

@property(nonatomic,assign)id delegate;
@property(nonatomic,retain) NSString *path;

in .m

@synthesize delegate;
@synthesize path;

创建一个方法,即

-(void)startDownloadImageWithUrl:(NSURL*)imageUrl withLocalFilePath:(NSSTring*)filePath{
       path = filePath;
       receiveData = [NSMutableData data];
       NSURLRequest *request = [[NSURLRequest alloc]initWithURL:imageUrl];
       NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request];
 }

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        [receiveData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
        [(id)delegate performSelector:@selector(SaveData:inFilePath:) withObject:receiveData withObject:path];
}

这将创建连接并开始在单独的类中下载您的图像。然后在你的mai ViewController中添加如下代码。

  - (void)updateImage {
          NSString *snake_ico_file = @"snake_img_icn_";
          NSString *filePath = [self dataFile:[snake_ico_file stringByAppendingString:@"0.jpg"]];
          int max_count = [[self readData] count];
          if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
                 for (int i = 0; i < max_count; i++) {
                 NSString *path = @"http://exitosus.no.de/drngoo/image/";
                 path = [path stringByAppendingFormat:@"%d",i];
                 NSString *ico_path = [path stringByAppendingFormat:@"/ico"];

                      //icon            
                 NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                 NSString *documentsDirectory = [paths objectAtIndex:0];
                 NSString *filename = [snake_ico_file stringByAppendingFormat:@"%d.jpg"];
                 NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:filename];

                 ImageDownLoad *imageDown = [[ImageDownLoad alloc]init];
                 imageDown.delegate = self;
                 [imageDown startDownloadImageWithUrl:[NSURL URLWithString:ico_path] withLocalFilePath:localFilePath];

            }
   }

每次图像下载完成时都会调用此方法。

   -(void)SaveData:(NSData*)data inFilePath:(NSString*)filePath{

          [data writeToFile:filePath atomically:YES];

   }

您的用户界面不会因此而延迟:)