2012-03-09 48 views
1

我從服務器獲取照片列表並顯示它們。我正在使用GCD來調用服務器調用。我知道它的工作原理,但是我想爲每個UIImageView添加一個UIActivityIndi​​cator來顯示它正在做什麼並且會出現。UIActivityIndi​​cator和GCD /線程

我不確定最好的方法是什麼。

代碼:

UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, photoView.frame.size.height)]; 
myScrollView.backgroundColor = [UIColor whiteColor]; 
myScrollView.pagingEnabled = TRUE; 
myScrollView.scrollEnabled = TRUE; 

myScrollView.frame = CGRectMake(myScrollView.frame.origin.x, myScrollView.frame.origin.y, (6 * THUMBNAIL_SIZE) + (PHOTO_VIEWER_OFFSET_COLUM_1 * 2), myScrollView.frame.size.height); 

//get list of photos 

for (int i = 0; i < 6; i++) 
{ 
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 

    dispatch_async(queue, ^{ 
     NSLog(@"async thread %@", [NSThread currentThread]); 

     //retrieveThumbnailedGeotaggedPhotoWithPhotoID will make a server call 
     MyUIImageView *myImageView = [[MyUIImageView alloc] initWithImage:[self retrieveThumbnailedGeoTaggedPhotoWithPhotoID:@"test"]]; 
     dispatch_sync(dispatch_get_main_queue(), ^{ 
      NSLog(@"main thread %@", [NSThread currentThread]); 

      myImageView.frame = CGRectMake(myImageView.frame.origin.x, myImageView.frame.origin.y, THUMBNAIL_SIZE, THUMBNAIL_SIZE); 
      myImageView.backgroundColor = [UIColor clearColor]; 
      myImageView.position = i; 
      myImageView.isEnlarged = FALSE; 
      myImageView.delegate = self; 

      int group = (i/6); 

      myImageView.frame = CGRectMake((i * myImageView.frame.size.width) + (PHOTO_VIEWER_OFFSET_COLUM_1 * ((group * 2) + 1)), 
              PHOTO_VIEWER_OFFSET_COLUM_1, 
              myImageView.frame.size.width, 
              myImageView.frame.size.height); 

      myScrollView.contentSize = CGSizeMake((myScrollView.frame.size.width * (group + 1)), myScrollView.contentSize.height); 

      [myScrollView addSubview:myImageView]; 
     }); 
    }); 
} 

我敢肯定,動畫和停止activityIndi​​cator必須在主線程上,但不知道如何將其納入。它需要在線程(但不是主線程)中的「retrieveThumbnailedGeoTaggedPhotoWithPhotoID」方法期間進行動畫處理。

+0

哪一部分是你不清楚?如何獲得主線程上的東西?或者使用活動指標?您已經解決了第一部分 - GCD的主隊列保證在主線程上運行。 – 2012-03-09 19:46:30

+0

在哪裏插入一個活動指標到代碼中,以便它在主線程上並且不會使我崩潰。在我看來,完美的地方是在異步線程,但它會崩潰(UI不在主線程=崩潰) – Padin215 2012-03-09 19:57:34

+0

我應該做的另一個dispatch_sync(dispatch_get_main_queue()之前,我讓服務器調用開始動畫,並再次正確在服務器調用停止動畫後? – Padin215 2012-03-09 19:58:18

回答

2

K,我重新下令:

for (int i = 0; i < 8; i++) 
{ 
    //create the subviews first 
    MyUIImageView *myImageView = [[MyUIImageView alloc] initWithFrame:CGRectMake((i * THUMBNAIL_SIZE) + (PHOTO_VIEWER_OFFSET_COLUM_1 * (((i/6) * 2) + 1)), 
                  PHOTO_VIEWER_OFFSET_COLUM_1, 
                  THUMBNAIL_SIZE, 
                  THUMBNAIL_SIZE)]; 

    myImageView.backgroundColor = [UIColor whiteColor]; 
    [myScrollView addSubview:myImageView]; 

    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 

    spinner.center = CGPointMake(THUMBNAIL_SIZE/2, THUMBNAIL_SIZE/2); 

    [myImageView addSubview:spinner]; 
    [spinner startAnimating]; 

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 

    dispatch_async(queue, ^{ 

     UIImage *myImage = [[UIImage alloc] init]; 
     //only make the server call in the async queue 
     myImage = [self retrieveThumbnailedGeoTaggedPhotoWithPhotoID:@"test"]; 

     dispatch_sync(dispatch_get_main_queue(), ^{ 
      NSLog(@"main thread %@", [NSThread currentThread]); 

      //MyUIImageView *myImageView = [[MyUIImageView alloc] initWithImage:myImage]; 
      [myImageView setImage:myImage]; 
      myImageView.frame = CGRectMake(myImageView.frame.origin.x, myImageView.frame.origin.y, THUMBNAIL_SIZE, THUMBNAIL_SIZE); 
      myImageView.backgroundColor = [UIColor clearColor]; 
      myImageView.position = i; 
      myImageView.isEnlarged = FALSE; 
      myImageView.delegate = self; 

      int group = (i/6); 

      myImageView.frame = CGRectMake((i * myImageView.frame.size.width) + (PHOTO_VIEWER_OFFSET_COLUM_1 * ((group * 2) + 1)), 
              PHOTO_VIEWER_OFFSET_COLUM_1, 
              myImageView.frame.size.width, 
              myImageView.frame.size.height); 

      myScrollView.contentSize = CGSizeMake((myScrollView.frame.size.width * (group + 1)), myScrollView.contentSize.height); 

      [spinner stopAnimating]; 
     }); 
    }); 
}