【问题标题】:loading a image from documents directory in iPhone从 iPhone 中的文档目录加载图像
【发布时间】:2010-11-04 11:46:42
【问题描述】:

我想从我的应用文档库中将图像加载到 UIImageView。我正在尝试使用以下代码,但它不起作用。

UIImageView *background = [[[UIImageView alloc] initWithFrame:CGRectMake(3, 10, 48, 36)] autorelease];

[background setImage:[[UIImage imageAtPath:[[NSBundle mainBundle] pathForResource:@"Thumbnail-small" ofType:@"jpg" inDirectory:@"/Users/nbojja/Library/Application Support/iPhone Simulator/User/Applications/60C2E4EC-2FE0-4579-9F86-08CCF078216D/Documents/eb43ac64-8807-4250-8349-4b1f5ddd7d0d/9286371c-564f-40b4-99bd-a2aceb00a6d3/9"]]] retain]];

有人可以帮我做这件事吗?谢谢...

【问题讨论】:

  • 为什么不在 UIImage 上使用 imageNamed?

标签: iphone


【解决方案1】:

斯威夫特 3:

let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
if let dirPath = paths.first {
let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("Image2.png")
let image = UIImage(contentsOfFile: imageURL.path)
// Do whatever you want with the image
}

【讨论】:

    【解决方案2】:
    NSString *docPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath=[NSString stringWithFormat:@"%@/image.png",docPath];
    
    BOOL fileExists=[[NSFileManager defaultManager] fileExistsAtPath:filePath];
    
    if (!fileExists)
        NSLog(@"File Not Found");
    else
        image = [UIImage imageWithContentsOfFile:filePath];
    

    【讨论】:

      【解决方案3】:

      使用[UIImage imageNamed:@"ThumbnailSmall.jpg"];

      这会加载你想要的我的想法。

      【讨论】:

        【解决方案4】:

        如果您真的想要从应用的文档文件夹中加载图像,您可以使用这个:

        NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
        NSString *docDirectory = [sysPaths objectAtIndex:0];
        NSString *filePath = [NSString stringWithFormat:@"%@/Thumbnail-small.jpg", docDirectory];
        background.image = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];
        

        但是,正如其他人在这里指出的那样,您可能希望从您的应用程序包中加载它,在这种情况下,其中任何一个都可以工作:

        background.image = [UIImage imageNamed:@"Thumbnail-small.jpg"];
        

        NSString *path = [[NSBundle mainBundle] pathForResource:@"Thumbnail-small" ofType:@"jpg"];
        background.image = [UIImage imageWithContentsOfFile:path];
        

        【讨论】:

          【解决方案5】:

          查看this related question,其中包含您想要的代码。

          【讨论】:

            【解决方案6】:

            我认为你真正需要的是:

            UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"myimagefile" ofType:@"png"]];
            [self.testView.imgView setImage:img];
            

            【讨论】:

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