作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个由 UIButtons 组成的缩略图查看器,并且希望每个按钮都有一个微调器,直到图像从云中填充为止。我以前曾用单个旋转器做过这个,但从未像这样集体做过。旋转器没有出现(也许是非主线程 UI 问题?)...我至少朝着正确的方向前进吗?提前致谢。
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_queue_t queue2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_apply(numberOfThumbs, queue, ^(size_t i){
Post *post = [arrayOfPosts objectAtIndex:i];
UIActivityIndicatorView *newSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UIButton *currentThumb = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[currentThumb setFrame:CGRectMake((105.5 * (i%3))+3.5, ((i/3)*105.5) + 3.5, 102, 102)];
[newSpinner setFrame:CGRectMake(54 - spinner.frame.size.width/2, 54 - spinner.frame.size.height/2, spinner.frame.size.width, spinner.frame.size.height)];
[currentThumb addSubview:newSpinner];
[newSpinner startAnimating];
[thumbnails addObject:currentThumb];
dispatch_async(queue2, ^(void){
[currentThumb setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:post.thumbUrl]]] forState:UIControlStateNormal];
[newSpinner stopAnimating];
[newSpinner removeFromSuperview];
[newSpinner release];
});
[currentThumb release];
});
dispatch_release(queue);
dispatch_release(queue2);
感谢 Daniel 和 Brad 的建议,下面的舞蹈似乎有效:
- (id)initWithArrayOfPosts:(NSArray*)posts
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
// Custom initialization
arrayOfPosts = [posts retain];
numberOfThumbs = [arrayOfPosts count];
oldDequeueFactor = 0;
oldYOffset = 0.0;
thumbnails = [[NSMutableArray alloc] initWithCapacity:15];
mainScrollView = [[UIScrollView alloc] init];
[mainScrollView setFrame:self.view.frame];
[mainScrollView setContentSize:CGSizeMake(self.view.frame.size.width, 109 * (ceilf((float)numberOfThumbs/3.0)))];
[mainScrollView setDelegate:self];
[mainScrollView setBackgroundColor:[UIColor whiteColor]];
[self setView:mainScrollView];
for (int i=0; i<numberOfThumbs; i++) {
UIActivityIndicatorView *newSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UIButton *currentThumb = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[currentThumb setFrame:CGRectMake((105.5 * (i%3))+3.5, ((i/3)*105.5) + 3.5, 102, 102)];
[newSpinner setFrame:CGRectMake(54 - newSpinner.frame.size.width/2, 54 - newSpinner.frame.size.height/2, newSpinner.frame.size.width, newSpinner.frame.size.height)];
[currentThumb addSubview:newSpinner];
[newSpinner startAnimating];
[thumbnails addObject:currentThumb];
[currentThumb release];
[mainScrollView addSubview:[thumbnails objectAtIndex:i]];
[self getImageDataAsynchronouslyForThumbnail:[thumbnails objectAtIndex:i] forIndex:i andRemoveSpinner:newSpinner];
}
}
return self;
}
- (void)getImageDataAsynchronouslyForThumbnail:(UIButton*)thumb forIndex:(int)index andRemoveSpinner:(UIActivityIndicatorView *)spinner
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_async(queue, ^(void){
Post *post = [arrayOfPosts objectAtIndex:index];
UIImage *thisImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:post.thumbUrl]]];
NSArray *args = [NSArray arrayWithObjects:thisImage, thumb, spinner, nil];
if ([args count] == 3) { //use this array to verify I have all info I need
dispatch_sync(dispatch_get_main_queue(), ^(void) {
[self setImage:[args objectAtIndex:0] ForThumbnail:[args objectAtIndex:1] andStopAnimatingSpinner:[args objectAtIndex:2]];
});
} else {
[self getImageDataAsynchronouslyForThumbnail:thumb forIndex:index andRemoveSpinner:spinner];
return;
}
});
dispatch_release(queue);
}
- (void)setImage:(UIImage*)image ForThumbnail:(UIButton *)thumb andStopAnimatingSpinner:(UIActivityIndicatorView *)spinner
{
[thumb setImage:image forState:UIControlStateNormal];
[spinner stopAnimating];
[spinner removeFromSuperview];
[spinner release];
}
最佳答案
是的,我相信你的问题是你正在从后台线程执行代码,UIKit不是线程安全的,所以你可能应该只在后台获取图像数据,然后使用performSelectorOnMainTHread来设置图像并停止动画,某些东西喜欢
-(void)setDataStopAnimating:(NSArray*)args
{
//get your arguments from the array and then run
[thumb setImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
[newSpinner stopAnimating];
[newSpinner removeFromSuperview];
[newSpinner release];
}
//then in your queue code
dispatch_async(queue2, ^(void){
NSArray *args; //fill the array with the arguments
[self performSelectorOnMainThread:@selector(setDataAndStopAnimating:) withObject:args]
});
关于ios - 正确使用 Grand Central Dispatch 与多个 UIActivityIndicatorViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7476636/
我是一名优秀的程序员,十分优秀!