Bazinga
Bazinga

Reputation: 2466

NSDocumentDirectory loading images

I was able to save my array of files in my NSDocumentDirectory. Here is what I used:

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDir = [paths objectAtIndex:0];
        NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", i]];
         ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation];
        UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
        //NSData *imageData = UIImagePNGRepresentation(image);
        [UIImageJPEGRepresentation(image, 1.0) writeToFile:savedImagePath atomically:YES];

My problem is how to load images with this format@"Images%d.png" into this code:

.h

@property (strong, nonatomic) NSMutableArray *images;

.m

    self.images = ??????

Upvotes: 0

Views: 2069

Answers (3)

suku
suku

Reputation: 237

for Saving Images to NSDocumentDirectory

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];//m_nImageIndex
        NSString *imagename = [NSString stringWithFormat:@"savedImage%d.png",m_nImageIndex];
        NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imagename];
        // Get PNG data from following method
        NSData *myData =     UIImagePNGRepresentation(image);
        // It is better to get JPEG data because jpeg data will store the location and other related information of image.
        [myData writeToFile:savedImagePath atomically:NO];
        //[UIImageJPEGRepresentation(image ,1) writeToFile:savedImagePath atomically:NO];

for retrieving Images

          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
          NSString *documentsDirectory = [paths objectAtIndex:0];
          NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"savedImage%d.png",indexPath.row]];
          UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];

          cell.backgroundView = [[UIImageView alloc] initWithImage:img];

Upvotes: 1

Michael Frederick
Michael Frederick

Reputation: 16714

You are saving a png extension as a jpg file. I believe this should work:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.jpg", i]];
    ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation];
    UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
    [UIImageJPEGRepresentation(image, 1.0) writeToFile:savedImagePath atomically:YES];

    // to load the image later:
    UIImage *imageFromDisk = [UIImage imageWithContentsOfFile:savedImagePath];

Upvotes: 0

Amar
Amar

Reputation: 13222

Here is the code to read the image names from bundle. Please modify bundle path to documents directory path. Code snippet will give you array of sorted image names also save you from using a for-loop to read names.

NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *dirContents = [fileManager contentsOfDirectoryAtPath:bundlePath error:nil];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"];
NSArray *unsortedImages = [dirContents filteredArrayUsingPredicate:filter];
NSArray *sortedImages = [unsortedImages 
                     sortedArrayUsingComparator:
                     ^NSComparisonResult (id obj1, id obj2){
                         return [(NSString*)obj1 compare:(NSString*)obj2 
                                                 options:NSNumericSearch];
                    }];

Friendly advice: Avoid storing any downloadable data/images in NSDocumentDirectory directory as app may get rejected. Use NSCachesDirectory instead. Please read this post and related documents on Apple's developer portal.

Cheers!!
Amar.

Upvotes: 0

Related Questions