2016-09-08 8 views
0

아래의 코드처럼 PHP를 통해 콜렉션 뷰에서 이미지를로드하려고합니다.컬렉션보기에서 이미지가 표시되지 않습니다.

@interface SearchMainPost() 
{ 

    NSMutableArray *myObject; 
    // A dictionary object 
    NSDictionary *dict; 
    // Define keys 
    NSString *imageid; 
    NSString *name; 
    NSString *path; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    // Define keys 

    imageid = @"videoImage"; 
    name = @"timeLineVideoUserName"; 
    path = @"TheIndex"; 

    // Create array to hold dictionaries 
    myObject = [[NSMutableArray alloc] init]; 


    NSData *jsonData = [NSData dataWithContentsOfURL: 
         [NSURL URLWithString:@"http://mywebSite/list/list.php"]]; 


    id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; 

    // values in foreach loop 
    for (NSDictionary *dataDict in jsonObjects) { 

     NSString *strImageID = [dataDict objectForKey:@"videoImage"]; 

     NSString *strName = [dataDict objectForKey:@"timeLineVideoUserName"]; 

     NSString *strPath = [dataDict objectForKey:@"TheIndex"]; 

     dict = [NSDictionary dictionaryWithObjectsAndKeys: 

       strImageID, imageid, 
       strName, name, 
       strPath, path, 
       nil]; 
     [myObject addObject:dict]; 


     NSLog(@"%@", strImageID); 
    } 
} 

그녀는 내가 컬렉션을 구현 한 방법입니다.

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { 
    return 1; 
} 


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
     return myObject.count; 
} 

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

    SearchMainPostCell *myCell = [collectionView 
           dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath]; 

    NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row]; 

    NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:path]]; 
    NSData *data = [NSData dataWithContentsOfURL:url]; 
    UIImage *img = [[UIImage alloc] initWithData:data]; 

    myCell.displayImage.image = img; 

    // myCell.displayDetail.text= [tmpDict objectForKey:name]; 


    return myCell; 

} 

내 문제는 이미지가 셀에 표시되지 않는다는 것입니다. 모든 콜렉션 뷰는 검은 색입니다. 콘솔에있는 정보가 표시되고 이미지 링크를 볼 수 있습니다. 콜렉션 뷰 셀이 설정되었으며 속성 검사기에서 식별자를 "myCell"로 설정합니다.

설명을 드려 죄송합니다. 이미지를 표시하는 방법을 다른 사람이 도와 주길 바랍니다.

미리 감사드립니다.

답변

0

비동기 작업의 경우 dispatch_async와 함께 GCD (Grand Central Dispatch).handle 백그라운드 작업을 사용해야하므로 tableview에서 URL을 요청하는 것이 비동기이어야합니다. 또한 viewDidLoad에서 PHP에서 JSON 데이터를 가져 와서 처리하면 데이터 다운로드를위한 UI가 고정됩니다. 데이터를 다운로드하는 좋은 방법이 아니므로 GCD를 사용하여 다운로드 할 수도 있습니다.

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

    SearchMainPostCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 

    NSDictionary *dict = [dataArray objectAtIndex:indexPath.row]; 

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    dispatch_async(queue, ^(void) { 

     NSURL *url = [NSURL URLWithString:[dict objectForKey:path]]; 

     NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; 

     UIImage* image = [[UIImage alloc] initWithData:imageData]; 
     if (image) { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       cell.imageView.image = image; 
       [cell setNeedsLayout]; 
      }); 
     } 
    }); 

    return cell; 
} 

편집 :

@interface ViewController() 
{ 
    NSCache *imageCache; 
} 

의 viewDidLoad에서 : collectionView에서

imageCache = [[NSCache alloc] init]; 

: cellForItemAtIndexPath :

if([imageCache objectForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]]) 
{ 
    cell.image.image = [imageCache objectForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]]; 
} 
else 
{ 
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    dispatch_async(queue, ^(void) { 

     NSURL *url = [NSURL URLWithString:[self.imageArray objectAtIndex:indexPath.row]]; 
     NSData *imageData = [NSData dataWithContentsOfURL:url]; 
     UIImage* image = [[UIImage alloc] initWithData:imageData]; 
     if (image) { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       [imageCache setObject:image forKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]]; 
       cell.image.image = image; 
       [cell setNeedsLayout]; 
      }); 
     } 
    }); 
} 
+0

감사합니다, 그것은 노력하고 있습니다. 그러나 이미지로드가 시작되고 이미지를 누르기 시작하면 이미지가 임의로 시작됩니다. –

+0

NSCache를 사용하여 이미지를 저장하고 다시 사용하십시오. 그렇지 않으면 이미지가 무작위로 변경되는 이유가 될 때마다 이미지를 다운로드해야합니다. – Jeyamahesan

+0

ur 도움을 주셔서 감사합니다 –

관련 문제