我从服务器获取照片列表并显示它们。我正在使用GCD来线程化服务器调用。我得到它的工作,但我想为每个UIImageView添加一个UIActivityIndicator,以显示它做了什么,并将出现。
我不确定最好的方法是什么。
代码:
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];
});
});
}
我非常确定动画和停止activityIndicator需要在主线程上,但不知道如何包含它。它需要在“retrieveThumbnailedGeoTaggedPhotoWithPhotoID”方法中进行动画处理,该方法位于线程中(但不是主线程)。
答案 0 :(得分: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];
});
});
}