2014-04-13 42 views
0

我使用UIScrollView顯示多個圖像。不過,我希望它自動刷新。使用解析自動刷新圖像

我想在每次顯示視圖控制器時刷新它。

使用我的代碼我必須每次都推動刷新按鈕,這只是無稽之談。

這是代碼:我該如何做到這一點?

- (IBAction)refresh:(id)sender 
{ 
    NSLog(@"Showing Refresh HUD"); 
    refreshHUD = [[MBProgressHUD alloc] initWithView:self.view]; 
    [self.view addSubview:refreshHUD]; 

    // Register for HUD callbacks so we can remove it from the window at the right time 
    refreshHUD.delegate = self; 

    // Show the HUD while the provided method executes in a new thread 
    [refreshHUD show:YES]; 

    PFQuery *query = [PFQuery queryWithClassName:@"TPhoto"]; 
    PFUser *user = [PFUser currentUser]; 
    [query whereKey:@"user" equalTo:user]; 
    [query orderByAscending:@"createdAt"]; 

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
     if (!error) { 
      // The find succeeded. 
      if (refreshHUD) { 
       [refreshHUD hide:YES]; 

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

       // The sample image is based on the work by http://www.pixelpressicons.com, http://creativecommons.org/licenses/by/2.5/ca/ 
       // Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators) 
       refreshHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]]; 

       // Set custom view mode 
       refreshHUD.mode = MBProgressHUDModeCustomView; 

       refreshHUD.delegate = self; 
      } 
      NSLog(@"Successfully retrieved %d photos.", objects.count); 

      // Retrieve existing objectIDs 

      NSMutableArray *oldCompareObjectIDArray = [NSMutableArray array]; 
      for (UIView *view in [photoScrollView subviews]) { 
       if ([view isKindOfClass:[UIButton class]]) { 
        UIButton *eachButton = (UIButton *)view; 
        [oldCompareObjectIDArray addObject:[eachButton titleForState:UIControlStateReserved]]; 
       } 
      } 

      NSMutableArray *oldCompareObjectIDArray2 = [NSMutableArray arrayWithArray:oldCompareObjectIDArray]; 

      // If there are photos, we start extracting the data 
      // Save a list of object IDs while extracting this data 

      NSMutableArray *newObjectIDArray = [NSMutableArray array]; 
      if (objects.count > 0) { 
       for (PFObject *eachObject in objects) { 
        [newObjectIDArray addObject:[eachObject objectId]]; 
       } 
      } 

      // Compare the old and new object IDs 
      NSMutableArray *newCompareObjectIDArray = [NSMutableArray arrayWithArray:newObjectIDArray]; 
      NSMutableArray *newCompareObjectIDArray2 = [NSMutableArray arrayWithArray:newObjectIDArray]; 
      if (oldCompareObjectIDArray.count > 0) { 
       // New objects 
       [newCompareObjectIDArray removeObjectsInArray:oldCompareObjectIDArray]; 
       // Remove old objects if you delete them using the web browser 
       [oldCompareObjectIDArray removeObjectsInArray:newCompareObjectIDArray2]; 
       if (oldCompareObjectIDArray.count > 0) { 
        // Check the position in the objectIDArray and remove 
        NSMutableArray *listOfToRemove = [[NSMutableArray alloc] init]; 
        for (NSString *objectID in oldCompareObjectIDArray){ 
         int i = 0; 
         for (NSString *oldObjectID in oldCompareObjectIDArray2){ 
          if ([objectID isEqualToString:oldObjectID]) { 
           // Make list of all that you want to remove and remove at the end 
           [listOfToRemove addObject:[NSNumber numberWithInt:i]]; 
          } 
          i++; 
         } 
        } 

        // Remove from the back 
        NSSortDescriptor *highestToLowest = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO]; 
        [listOfToRemove sortUsingDescriptors:[NSArray arrayWithObject:highestToLowest]]; 

        for (NSNumber *index in listOfToRemove){ 
         [allImages removeObjectAtIndex:[index intValue]]; 
        } 
       } 
      } 

      // Add new objects 
      for (NSString *objectID in newCompareObjectIDArray){ 
       for (PFObject *eachObject in objects){ 
        if ([[eachObject objectId] isEqualToString:objectID]) { 
         NSMutableArray *selectedPhotoArray = [[NSMutableArray alloc] init]; 
         [selectedPhotoArray addObject:eachObject]; 

         if (selectedPhotoArray.count > 0) { 
          [allImages addObjectsFromArray:selectedPhotoArray]; 
         } 
        } 
       } 
      } 

      // Remove and add from objects before this 
      [self setUpImages:allImages]; 

     } else { 
      [refreshHUD hide:YES]; 

      // Log details of the failure 
      NSLog(@"Error: %@ %@", error, [error userInfo]); 
     } 
    }]; 
} 
+0

放在'viewDidAppear'' [self refresh:nil]' –

回答

0

呼叫從viewDidAppear在您的視圖控制器的刷新方法。

但是,如果這種情況經常發生,您應該考慮將圖像保存在本地緩存中,並且只有在服務器上顯示新內容時才刷新它們。