2016-02-11 26 views
-2

我正在做一個json查詢來獲取圖像網址的數組。我想在uicollection視圖中顯示圖像。但我錯過了一些東西。我想我必須解析數組並將nsurl設置爲每個項目。然後把每個項目放在nsdata中。但我不確定。我也不想使用第三方軟件。圖像url到UI的數組collectionview

這是我的代碼示例。

- (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; 
} 

這裏我finalImageArray的輸出,我想申請uicollectionview,這裏是日誌數據: {( 「http://www.test/inventory/images/bball.jpg」, 「http://www.test/images/bird%20tank_0.jpg」 )}

所以我錯過像nsurl或nsdata,或什麼。我如何獲得這個數組的圖像url顯示在一個uicollectionview?謝謝您的幫助!

+0

我添加了sdwebimage。我添加了'[cell.imageView sd_setImageWithURL:[NSURL URLWithString:[finalImageArray objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@「icon-29.png」]];但仍然沒有任何想法? –

回答

0

只需使用數組的更新數據重新加載Collection視圖。正如您在後臺線程中所請求的。現在,您需要重新加載主線程上的數據。

- (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]; 
    }); 
} 

希望它能幫助你。

+0

我沒有得到reloadData的已知類方法 –