【问题标题】:Loading an Image from documents directory从文档目录加载图像
【发布时间】:2012-01-27 23:39:04
【问题描述】:

我的图片(.png)无法显示。

我检查它是否存在于我使用的路径中,并且确实存在。但是没有图像出现。

这是我一直在尝试的:

NSArray  *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir  = [documentPaths objectAtIndex:0];

NSString *outputPath    = [documentsDir stringByAppendingPathComponent:[fileList objectAtIndex:indexPath.row]];

NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];



NSString *imgPath = [[outputPath stringByReplacingOccurrencesOfString:@"mov" withString:@"png"]retain];


if ([fileManager fileExistsAtPath:imgPath]) 
{
    NSLog(@"FOUND IMG");
    NSLog(imgPath);
}


NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imgPath]];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 120)];
[imgView setImage:thumbNail];

[cell addSubview:imgView];
[imgView release];

确认文件存在的调试输出

FOUND IMG
2011-12-26 12:42:23.066 iGeo2[412:707] /var/mobile/Applications/1A2B0D85-CDB1-4E4B- 
9509-EF68B1A0E720/Documents/0.009000.png

我有点困惑。有人看到我在哪里犯了错误吗?

非常感谢, -代码

我尝试了其他几种方法,但都没有出现。

【问题讨论】:

  • 您可以在这里得到答案:stackoverflow.com/questions/6663511/…
  • 要使用NSFileManager 简单地使用说服方法:NSFileManager *fileManager = [NSFileManager defaultManager] 通常检查文档以了解基于类的说服方法。如果可能的话,也使用 ARC,它确实使开发代码更简单。

标签: iphone objective-c ios xcode


【解决方案1】:

尝试使用

NSData *imgData = [NSData dataWithContentsOfFile:imgPath];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];

NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:imgPath]];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];

【讨论】:

  • 如何显示文档目录中的 GIF 图片?
【解决方案2】:

您应该使用[NSURL fileURLWithPath:imgPath] 而不是[NSURL URLWithString:imgPath]。图片路径不是有效的 url,它需要一个 file:// 前缀。

【讨论】:

    【解决方案3】:

    您需要提供正确的文件网址

    NSURL *imgURL = [NSURL fileURLWithPath:imgPath];
    

    然后使用正确的 imgURL var 初始化您的图像,最后将图像添加到您需要执行以下操作的单元格中

    [cell.contentView addSubview:imgView];
    

    希望它能解决你的问题。

    【讨论】:

      【解决方案4】:
      imageView.image = [UIImage imageWithContentsOfFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Filename"]];
      

      【讨论】:

        【解决方案5】:

        斯威夫特 4

        代码:

        extension FileManager {
            class func getImage(named: String) -> UIImage? {
                let imagePath = getImagePath(named: named)
                return UIImage(contentsOfFile: imagePath)
            }
        
            class func getImagePath(named: String) -> String {
                let fileManager = FileManager.default
                let documentDir = try! fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
                return documentDir.appendingPathComponent(named).path
            }
        }
        

        用法:

        let image = FileManager.getImage(named: "image.png")
        

        【讨论】:

          【解决方案6】:

          你的网址是完美的。

          您需要使用以下行初始化图像

          UIImage *thumbNail=[UIImage imageWithContentsOfFile:imagePath];
          

          现在做这个

          [imgView setImage:thumbNail];
          
          [cell addSubview:imgView];
          

          【讨论】:

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