我无法从Flickr有效下载照片数量。我用过了主队列。但这还不够。打击是我的代码。
-(void)flickrNewPhotosFromArray:(NSArray *)photos withCompletionHandler: (void (^) (NSArray*))completionHandler{
dispatch_queue_t fetchQ = dispatch_queue_create("fetch photos1", NULL);
dispatch_async(fetchQ, ^{
NSMutableArray *arrayPhotos = [[NSMutableArray alloc] init];
for(int i = 0; i < photos.count; i ++){
NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc] init];
NSURL *url = [FlickrFetcher URLforPhoto:photos[i] format:FlickrPhotoFormatLarge];
NSString *urlString = [url absoluteString];
NSData * data = [[NSData alloc] initWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
CGFloat width = image.size.width;
CGFloat height = image.size.height;
[mutableDict setObject:urlString forKey:@"img"];
[mutableDict setObject:[NSNumber numberWithInt:width] forKey:@"w"];
[mutableDict setObject:[NSNumber numberWithInt:height] forKey:@"h"];
[arrayPhotos addObject:mutableDict];
}
dispatch_async(dispatch_get_main_queue(), ^{
if (completionHandler) {
completionHandler(arrayPhotos);
}
});
});
}
我需要检查图像的宽度和高度以满足水流收集视图的要求,因为我下载的字典不提供这些。 感谢您的帮助。
答案 0 :(得分:0)
我想我自己得到了答案。只需使用组队列来处理!答案如下!仍然欢迎给我建议或提供更好的答案!感谢
-(void)flickrNewPhotosFromArray:(NSArray *)photos withCompletionHandler: (void (^) (NSArray*))completionHandler{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
dispatch_group_t group = dispatch_group_create();
__block NSMutableArray *arrayPhotos = [[NSMutableArray alloc] init];
for(int i = 0; i < photos.count; i ++){
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc] init];
NSURL *url = [FlickrFetcher URLforPhoto:photos[i] format:FlickrPhotoFormatLarge];
NSString *urlString = [url absoluteString];
NSData * data = [[NSData alloc] initWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
CGFloat width = image.size.width;
CGFloat height = image.size.height;
[mutableDict setObject:urlString forKey:@"img"];
[mutableDict setObject:[NSNumber numberWithInt:width] forKey:@"w"];
[mutableDict setObject:[NSNumber numberWithInt:height] forKey:@"h"];
[arrayPhotos addObject:mutableDict];
});
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
if (completionHandler) {
completionHandler(arrayPhotos);
}
});
});
}