图片数组 url 到 ui collectionview
Array of image url to ui collectionview
我正在执行 json 查询以获取图像数组 url。我想在 uicollection 视图上显示图像。但我错过了一些东西。我想我必须通过数组进行解析并将 nsurl 设置为每个项目。然后将每一项放入nsdata。但我不确定。我也不想使用 3rd 方软件。
这是我的代码示例。
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:
coProFeedURL];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
}
///new return all company data array
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray * getProductsArray = [json objectForKey:@"CompanyProduct"]; //2 get all product info
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"companyID = %@", passCoData];//added create filter to only selected company
NSArray * filteredProductArray = [getProductsArray filteredArrayUsingPredicate:predicate];//only products for selected company
///doing parsing here to get array of image urls
finalImageArray = [filteredProductArray allObjects];//original
NSLog(@" url images :%@", finalImageArray);
//NEED TO GET FINALIMAGEARRAY TO NSURL TYPE AND SET TO IMAGE?
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return finalImageArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"Cell";//test form file works
ItemCollectionViewCell *cell = (ItemCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];//test form file works
///then add the finalImageArray?
return cell;
}
这是我想应用于 uicollectionview 的 finalImageArray 的输出,这是日志数据:
{(
"http://www.test/inventory/images/bball.jpg",
“http://www.test/images/bird%20tank_0.jpg”
)}
我是不是漏掉了 nsurl 或 nsdata 之类的东西。我如何让这个图像数组 url 显示在 uicollectionview 上?感谢您的帮助!
只需使用数组的更新数据重新加载集合视图。正如您在后台线程上请求的那样。现在,您需要在主线程上重新加载数据。
- (void)fetchedData:(NSData *)responseData {
//YOUR CODE ...
finalImageArray = [filteredProductArray allObjects];//original
NSLog(@" url images :%@", finalImageArray);
//NEED TO GET FINALIMAGEARRAY TO NSURL TYPE AND SET TO IMAGE?
dispatch_async(dispatch_get_main_queue(), ^{
[collectionView reloadData];
});
}
希望对你有所帮助。
我正在执行 json 查询以获取图像数组 url。我想在 uicollection 视图上显示图像。但我错过了一些东西。我想我必须通过数组进行解析并将 nsurl 设置为每个项目。然后将每一项放入nsdata。但我不确定。我也不想使用 3rd 方软件。
这是我的代码示例。
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:
coProFeedURL];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
}
///new return all company data array
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray * getProductsArray = [json objectForKey:@"CompanyProduct"]; //2 get all product info
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"companyID = %@", passCoData];//added create filter to only selected company
NSArray * filteredProductArray = [getProductsArray filteredArrayUsingPredicate:predicate];//only products for selected company
///doing parsing here to get array of image urls
finalImageArray = [filteredProductArray allObjects];//original
NSLog(@" url images :%@", finalImageArray);
//NEED TO GET FINALIMAGEARRAY TO NSURL TYPE AND SET TO IMAGE?
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return finalImageArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"Cell";//test form file works
ItemCollectionViewCell *cell = (ItemCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];//test form file works
///then add the finalImageArray?
return cell;
}
这是我想应用于 uicollectionview 的 finalImageArray 的输出,这是日志数据: {( "http://www.test/inventory/images/bball.jpg", “http://www.test/images/bird%20tank_0.jpg” )}
我是不是漏掉了 nsurl 或 nsdata 之类的东西。我如何让这个图像数组 url 显示在 uicollectionview 上?感谢您的帮助!
只需使用数组的更新数据重新加载集合视图。正如您在后台线程上请求的那样。现在,您需要在主线程上重新加载数据。
- (void)fetchedData:(NSData *)responseData {
//YOUR CODE ...
finalImageArray = [filteredProductArray allObjects];//original
NSLog(@" url images :%@", finalImageArray);
//NEED TO GET FINALIMAGEARRAY TO NSURL TYPE AND SET TO IMAGE?
dispatch_async(dispatch_get_main_queue(), ^{
[collectionView reloadData];
});
}
希望对你有所帮助。