-1

I have array of images which contain fullpath of image. I stored images in document directory. The problem is when i scroll down the image load time in collection view is slow so scrolling is also very poor. Referencing this code to make it faster.

But the problem is images not displayed in cell. My collection view code is

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    AlbumImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AlbumImageCell" forIndexPath:indexPath];
    cell.albumImage.image=nil;
    cell.layer.shouldRasterize = YES;
    cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
    if (cell == nil) {
    cell = [[[NSBundle  mainBundle] loadNibNamed:@"AlbumImageCell" owner:self options:nil] objectAtIndex:0];
    }
NSString *path=[documentImage objectAtIndex:indexPath.row];

NSURL *url = [NSURL URLWithString:path];
// note that this can be a web url or file url

ImageRequest *request = [[ImageRequest alloc] initWithURL:url];

UIImage *image = [request cachedResult];

if (image) {
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:127];
    imageView.image = image;
    cell.albumImage.image=image;
} else {
    [request startWithCompletion:^(UIImage *image, NSError *error) {
        if (image && [[collectionView indexPathsForVisibleItems] containsObject:indexPath]) {
            [self.album_ImageView reloadItemsAtIndexPaths:@[indexPath]];
        }
    }];
}
return cell;
}

I got null in UIImage. Please help me to solve this.

Community
  • 1
  • 1
Mitesh Dobareeya
  • 970
  • 1
  • 11
  • 36

4 Answers4

1
NSString *path=[documentImage objectAtIndex:indexPath.row];
 NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:path isDirectory:NO];
UIImageView *sample=[[UIImageView alloc] init];
[sample sd_setImageWithURL:fileURL placeholderImage:[UIImage imageNamed:@"PHOTO.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

    if (image == nil) {
        UIImage *localImage = [UIImage imageNamed:@"PHOTO.png"];
        if (localImage == nil) {
            localImage = [UIImage imageNamed:@"PHOTO.png"];
        }

        sample.image = localImage;
    }
    cell.albumImage.image=image;
}];
Mitesh Dobareeya
  • 970
  • 1
  • 11
  • 36
-1

Ok, you can use this,

if you want to load images quickly in UICollectionView use 'EGOImageView', EGOImageView is Fast downloading images and also scroll is flexible, Please refer following link Download the EGOImageView Loader,

https://github.com/enormego/EGOImageLoading

Download the ZIP file, then integrate the following classes from your code,

  • EGOCahce
  • EGOImageLoader
  • EGOImageButton
  • EGOImageView

if you Integrate all the Class, Import the classes to UICollectionView Class,

#import "EGOImageView/EGOImageView.h"
#import "EGOImageLoader/EGOImageLoader.h"

if you import class, then declare EGOImageView example given below,

@property (retain, nonatomic) EGOImageView *stickerImageView;

In below code use to '-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath' get the url and paste the code,

NSURL *url = [NSURL URLWithString:path];
   if (url)
       [cell.stickerImageView setImageURL:url];
   else
       [cell.stickerImageView setImage:@"placeholderImage"];

its Working for me...

Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30
-1

I highly recommend this library for loading images in collection view cell or table view using NSURL:

SDWebImage

As far as I know, it's one of the most commonly used asynchronous image downloader and probably uses some internal caching mechanism. You need to use import different category headers based on the UI element where you want to show the image. In this case, you need to include

#import <SDWebImage/UIImageView+WebCache.h>

Please find the SAMPLE code I have shown below which I have used for setting the image view for each and every cell within a collection view cell.

NSURL *imageURL = [NSURL URLWithString:@"http://www.you-url-goes-here.png"];

[_sampleImageView sd_setImageWithURL:imageURL placeholderImage:[UIImage defaultPlaceHolderImage] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

        if (image == nil) {
            UIImage *localImage = [UIImage someLocalImage];
            if (localImage == nil) {
                localImage = [UIImage defaultPlaceHolderImage];
            }

            _sampleImageView.image = localImage;
        }
}];
Xartec
  • 2,369
  • 11
  • 22
Arjun K P
  • 2,081
  • 4
  • 20
  • 33
  • But i am storing image locally and i have array of imagepath. imagepath=>/Users/apple/Library/Developer/CoreSimulator/Devices/3D12A29C-32D2-4D17-B20A-E86BC46D3D73/data/Containers/Data/Application/A02525C5-FF55-44FD-AB71-450F2C50FC31/Documents/1456122234660.550049.png Please can u suggest me how can i display image in collectionview cell using SDWebImage – Mitesh Dobareeya Feb 22 '16 at 07:37
  • @MiteshDobareeya What i am suggesting is that you don't need to store image locally. I think that part is managed by SDWebcache library itself as it has its own internal caching mechanism. Like i mentioned in the code segment, you can set image to imageView using the `sd_setImageWithURL` method. I have added the completion block in the example where you need perform what needs to be done when the image is not fetched within the timespan. But, the image will be loaded as you loaded as you scroll and some caching mechanism is included to reuse the image. – Arjun K P Feb 22 '16 at 07:55
  • @MiteshDobareeya Well, if you are so peculiar for NSURL with local image. Try these links - https://github.com/rs/SDWebImage/issues/697 and http://stackoverflow.com/questions/12768264/how-to-get-filesystem-path-to-image-cached-with-sdwebimage-ios. But I believe this is not required. – Arjun K P Feb 22 '16 at 07:57
  • i don't use web images. I stored my application images in document directory and i want to display that images in collectionview.But it load images very slow when i scroll down please give idea regarding this. – Mitesh Dobareeya Feb 22 '16 at 08:08
  • Did you tried the setter method i mentioned in the sample code with your url which you mentioned in your question could be local file url or web url ? – Arjun K P Feb 22 '16 at 08:27
  • Yes i did it but image not displayed in cell. Please can you edit your sample code as i want to display image in collectioview cell. – Mitesh Dobareeya Feb 22 '16 at 09:44
  • Kindly update me the code you have tried in your question. – Arjun K P Feb 22 '16 at 09:59
  • yaa its working brother. i just forget to set isDirectory=NO; i post my answer please you check it. – Mitesh Dobareeya Feb 22 '16 at 10:20
  • comment if any change..can you suggest me how will i load all images before i scroll.Currently its working smooth but only thing is at scrolling time it takes may be 1 or 2 sec to load images. for making view more efficient. – Mitesh Dobareeya Feb 22 '16 at 10:30
  • Good to know, it helped, you can upvote my answer as well :). usually it takes some minor seconds in loading images. Actually, this minor delay in image loading is expected from an app unless you are accessing directly i.e something like `[UIImage imageNamed:@""];` if you are trying to load images for each and every cell within a tableview. – Arjun K P Feb 22 '16 at 12:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104174/discussion-between-mitesh-dobareeya-and-arjun-k-p). – Mitesh Dobareeya Feb 22 '16 at 13:06
  • Hi @ArjunKP I am using **sd_setImageWithURL** in my Application to load the images in UITableView. But it is very slow when I scroll the View little faster than normal scrolling speed. Can you suggest me something in this ? – UGandhi Jan 21 '17 at 10:05
-1

Buddy just go with the AFNetworking library.It is easy to use and You images will be fetched easy and it will be cached automatically and Your collection won't stuck.You will get hight performance of scrolling Download it from here https://github.com/AFNetworking/AFNetworking

UIImageView+AFNetworking.h

Use above category to fetch images

Anil solanki
  • 962
  • 4
  • 20