ScrollView:如何从Documents加载图像

时间:2013-12-03 10:36:17

标签: ios uiscrollview nsdocumentdirectory

我在iphoneSimlator / 7.0 /..../ Documents / Archive /中保存了一些图像 现在我正在进行滚动视图 如何从Documents / Archive /加载图像?我需要在scrollview上显示这些图像。

3 个答案:

答案 0 :(得分:0)

只需使用NSFileManager,如下所示:

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

它将为您提供一个数组,其中包含给定路径中所有文件的所有文件路径。

从主程序包加载可能正常,因为NSBundle正在为您缓存图像。你可以使用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];
}

如果你最终发现需要使用线程,那么我会使用GCD线程来加载图像,但是然后将该图像插入到我在示例代码中创建的同一个NSCache对象中。

答案 1 :(得分:0)

我刚刚把逻辑放在这里,因为你是新手。

    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);
    }

答案 2 :(得分:0)

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];

}