ios 从文档目录加载图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8635909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:01:51  来源:igfitidea点击:

Loading an Image from documents directory

iphoneobjective-ciosxcode

提问by YuAo

I cant get my Image(.png) to display.

我无法显示我的 Image(.png)。

I check that it exists at the path I use, and it does. But no image appears.

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

Here is what I've been trying:

这是我一直在尝试的:

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

The debug output that confirms the file exists

确认文件存在的调试输出

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

I'm a little baffled. Anybody see where I've made the mistake?

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

Many Thanks, -Code

非常感谢,-代码

I've tried a few other methods, but nothing ever appears.

我尝试了其他一些方法,但没有出现任何内容。

采纳答案by iHunter

You should use [NSURL fileURLWithPath:imgPath]instead of [NSURL URLWithString:imgPath]. Image path is not a valid url, it needs a file://prefix.

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

回答by YuAo

Try to use

尝试使用

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

or

或者

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

回答by Arslan

You need to give proper file url which will be

您需要提供正确的文件网址,这将是

NSURL *imgURL = [NSURL fileURLWithPath:imgPath];

then initialize your image with proper imgURL var and finally to add the image to your cell you need to do the following

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

[cell.contentView addSubview:imgView];

I hope it may solve your problem.

我希望它可以解决您的问题。

回答by Bobby

imageView.image = [UIImage imageWithContentsOfFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Filename"]];

回答by Zaid Pathan

Swift 4

斯威夫特 4

Code:

代码:

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

Usage:

用法:

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

回答by Pankaj purohit

Your url is perfect.

你的网址是完美的。

You need to initialise image with following line

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

UIImage *thumbNail=[UIImage imageWithContentsOfFile:imagePath];

now do this

现在做这个

[imgView setImage:thumbNail];

[cell addSubview:imgView];