【问题标题】:ScrollView:how to load images from DocumentsScrollView:如何从文档中加载图像
【发布时间】:2013-12-03 10:36:16
【问题描述】:

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

【问题讨论】:

标签: ios uiscrollview nsdocumentdirectory


【解决方案1】:

只需使用 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 对象中。

【讨论】:

  • image_path 是存储在数组中的图像的 keyValue。--- 因为您要获取多个图像,所以您必须将所有图像路径保存在数组中。
  • 我是新来的,所以这样问......你在哪里声明了那个数组
  • 我复制了 20 张图像并粘贴到文档/存档中。我需要在我的滚动视图中加载这些图像。我做了滚动视图,但需要在滚动视图上显示图像。
  • 添加了另一个答案,检查
【解决方案2】:
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];

}

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-01-27
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 2011-11-15
      • 2023-04-06
      • 1970-01-01
      • 2013-08-27
      相关资源
      最近更新 更多