2012-03-09 74 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]; 
     }); 
    }); 
}