ScrollView:how to load images from Documents

221 Views Asked by At

I saved some images in iphoneSimlator/7.0/..../Documents/Archive/.
Now i am working on scroll view.
How to load images from Documents/Archive/ ? I need to display these images on scrollview.

3

There are 3 best solutions below

0
On BEST ANSWER
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSArray *filePathsArray = [[NSFileManager defaultManager]                                                       
  subpathsOfDirectoryAtPath:documentsDirectory  error:nil];






for (int i=3; i<[filePathsArray count]; i++)
{    
    NSString *strFilePath = [filePathsArray objectAtIndex:i];

    NSString *fullpath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,strFilePath];

    NSLog(@"array with paths of image files in the Document Directory %@", fullpath);


    UIImage *myImage = [UIImage imageWithContentsOfFile:fullpath];

    NSLog(@"array with paths of image files in the Document Directory %@", myImage);

    UIImageView *img = [[UIImageView alloc] init];
    img.image = myImage;


    CGFloat xOrigin = (i - 3) * 320.0f;

    img.frame = CGRectMake(xOrigin,-64,320,117);
    [_scrollViewProperty  addSubview:img];

}
0
On

I have just put the logic out here, as you are newbie.

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    self.scrollView.contentSize = CGSizeMake(320, 465);
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];

    int Y = 0;

    // here give you image name is proper so we can access it by for loop.



    NSString *dataPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Archive"];
    NSArray *arrFilePath=[[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"" error:nil];
    for(int i=0;i<[arrFilePath count];i++)
    {
        NSString *fileName = [arrFilePath objectAtIndex:i];
        NSString *path = [dataPath stringByAppendingPathComponent:fileName];
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, Y, 100, 100)] ;
        [imageView setImage: image];
        [self.scrollView addSubview: imageView];
        Y = Y + imageView.frame.size.height+5;
        if(y > 465)
            self.scrollView.contentSize = CGSizeMake(320, y);
    }
4
On

Just use NSFileManager, something like this:

[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];

It will give you an array with all the file paths for all files in the given path.

Loading from the main bundle probably works fine because NSBundle is caching the images for you. You can do the same yourself using NSCache.

static NSCache *cache = nil;
if (!cache) {
    cache = [[NSCache alloc] init];
    [cache setCountLimit:10]; // check how much RAM your app is using and tweak this as necessary. Too high uses too much RAM, too low will hurt scrolling performance.
}

NSString *path = [[arr_images objectAtIndex:indexPath.row]valueForKey:@"Image_path"];
if ([cache objectForKey:path]) {
    img.image=[cache objectForKey:path];
} else {
    img.image=[UIImage imageWithContentsOfFile:path];
    [cache setObject:img.image forKey:path];
}

If in the end you do find you need to use threading, then I would use a GCD thread to load the image, but then insert that image into the same NSCache object I created in my sample code here.