1
votes

My project is having image sharing functionality. In this functionality my app asks the user from which library you want to share.

I wrote below code to assign image, retrieved from Default library/Custom library.

  • Device default library

    imgVwMediaFile.image = [UIImage imageWithCGImage:[asset thumbnail]; //(ALAsset *)asset

  • Custom library (images are at Documents/image/user)

    imgVwMediaFile.image = [UIImage imageWithContentsOfFile:path]; //(NSString*)path

App displyas this images in collection view having custom cell.

When i select app custom library then images are retrieves from Documents directory ,but imagewithcontentofFile cosuming around 90 to 100 Mb memory.

In other case when i select app default libray, it is not cosuming more then 8 or 10 Mb memory.

I tried diffrent code from stack Q/A for custom library, but still memory issue is there.

-1-
CGImageRef iref = [[UIImage imageNamed:asset] CGImage] ;
imgVwMediaFile.image=[UIImage  imageWithCGImage:iref];
iref=nil

-2-
NSData *imageData = [[NSData alloc] initWithContentsOfFile:path];
imgVwMediaFile.image=[UIImage imageWithData:imageData];
imageData=nil

-3-
imgVwMediaFile.image=[UIImage imageNamed:path];

So please guide me that, how can i load images from document dir ?

What are the best way to load images from document dir without increasing memory load ?

2
imageWithContentsOfFile simply loads the image from resource bundle with no caching.... better use imageWithContentsOfFile. - Tanuj

2 Answers

1
votes

The problem is that yhe images you have saved to disk are large, so loading lots of the, to display in a collection takes a lot of memory. You use thumbnails from the built in library so you see a different result.

Ideally you would save a folder of thumbnails in your custom library and display those, then, when an image is selected, get the corresponding full size image to use (this is the approach basically all photo libraries use). Alternatively you can resize the images on-the-fly, but your scrolling is basically guaranteed to suck.

-1
votes
    UIImage *img = [UIImage imageWithContentsOfFile:path];