0

I am trying to fetch images from URL and displaying it on collection view how to do it ? I have used following code but didnt get answer

(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {    
collectionGalleryTechrCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];    
// NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:Url];
//UIImage *image = [UIImage imageWithData:imageData];
//CollectionViewImage.image = image;    
NSURL  *urlBack = [NSURL URLWithString:@"http://ssk.com/zpschool1/web/gallery/download%20(4).jpg"];
 NSData *urlDataBack = [NSData dataWithContentsOfURL:urlBack];
UIImage *imageBack = [UIImage imageWithData:urlDataBack];  

cell.myImg.image =imageBack;
            //cell.myImg.image = [UIImage imageWithData:ImgURL];
            cell.myImg.userInteractionEnabled=YES;    

    return cell;
}
sumi
  • 3
  • 4

2 Answers2

0

Loading and displaying image asynchronously is better then getting NSData from URL and displaying imageWithData in UICollectionView or UITableView. You can check out this answer given in the link below : https://stackoverflow.com/a/34041518/9332509

Krishna Maru
  • 164
  • 13
0
    NSString *strImg_Path = [[NSString alloc]init];
strImg_Path = [NSString stringWithFormat:@"%@",[NSURL URLWithString:@"http://ssk.com/zpschool1/web/gallery/download%20(4).jpg"];
];

[cell.myImg sd_setImageWithURL:[NSURL URLWithString:strImg_Path]
                      placeholderImage:[UIImage imageNamed:@"placeholer_image"]];

make sure placeholer_image for default image and use framework - SDWebImage for lazy loading.

Use Url from your array for dynamic instead of static Url.

sumi
  • 3
  • 4
Akshay Degada
  • 139
  • 2
  • 13