2014-10-27 19 views
0

在通過CKAssets使用來自Cloudkit的數據後,我無法再呈現collectionview單元格。我以前使用加載在我桌面上的文件夾中的圖像進行初始測試。我現在使用Cloudkit,並使用這些相同的圖像通過CK儀表板創建了一些測試記錄。我成功地能夠查詢CK數據庫並檢索預期記錄。然後我改變了我的代碼來填充單元格的模型數據以使用CK數據。該數據以前來自本地檢索的圖像。從日誌記錄中我可以看到,我正在從CK成功獲取數據,包括圖像。我還可以從記錄中看到,我的自定義CV單元不再被啓動。從我所知道的來看,我的代碼看起來很好,基於我在網上看到的例子。Collectionview單元格不會使用CKAsset呈現

任何人都可以幫助我嗎?謝謝!

模型中的指定初始化...

- (instancetype)initImagesForSelection:(NSString *)selectionType { 

    self = [super init]; 

    if (self) { 
     CKDatabase *publicDatabase = [[CKContainer defaultContainer] publicCloudDatabase]; 
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ImageDescription = 'description'"]; 
     CKQuery *query = [[CKQuery alloc] initWithRecordType:@"ImageData" predicate:predicate]; 

     [publicDatabase performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) { 
      // handle the error 
      if (error) { 
       NSLog(@"Error: there was an error querying the cloud... %@", error); 
      } else { 
       // any results? 
       if ([results count] > 0) { 
        NSLog(@"Success querying the cloud for %lu results!!!", (unsigned long)[results count]); 
        for (CKRecord *record in results) { 
         ImageData *imageData = [[ImageData alloc] init]; 
         CKAsset *imageAsset = record[@"Image"]; 
         imageData.imageURL = imageAsset.fileURL; 
         NSLog(@"asset URL: %@", imageData.imageURL); 
         imageData.imageName = record[@"ImageName"]; 
         //imageData.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageAsset.fileURL]]; 
         imageData.image = [UIImage imageWithContentsOfFile:imageAsset.fileURL.path]; 
         NSLog(@"image size height:%f, width:%f", imageData.image.size.height, imageData.image.size.width); 
         [self.imageDataArray addObject:imageData]; 
        } 
        NSLog(@"imageDataArray size %lu", (unsigned long)[self.imageDataArray count]); 
       } 
      } 
     }]; 
    } 

    return self; 
} 

的CollectionView視圖 - 控制從Cloudkit拉低數據之前的工作完美...

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; // string value identifier for cell reuse 
    ImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; 
    NSLog(@"cellForItemAtIndexPath: section:%ld row:%ld", (long)indexPath.section, (long)indexPath.row); 
    cell.layer.borderWidth = 1.0; 
    cell.layer.borderColor = [UIColor grayColor].CGColor; 

    ImageData *imageData = [self.imageLoadManager imageDataForCell:indexPath.row]; 

    cell.imageView.image = imageData.image; 

    cell.imageView.contentMode = UIViewContentModeScaleAspectFit; 

    return cell; 
} 

回答

1

好吧,我想通了這一點。我的代碼實際上工作。由於來自cloudkit的數據存在多線程/異步下載問題,collectionview未顯示。我打開相機按鈕拍攝一張照片,刷新了簡歷,並出現了簡歷中的所有內容。我只需要使用多線程,以便在圖像下載時開始渲染。

相關問題