【问题标题】:How to get the local saved UIImage path to UIImage?如何获取本地保存的 UIImage 到 UIImage 的路径?
【发布时间】:2013-03-06 21:10:17
【问题描述】:

我正在使用以下代码保存我的图像 ---

#define DOCUMENTS_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]


NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
NSString *caldate = [now description];


NSString *filePath= [NSString stringWithFormat:@"%@/%@.jpg", DOCUMENTS_FOLDER,caldate];
imageurl = [NSURL fileURLWithPath:filePath];
dataImage = UIImagePNGRepresentation(myimage);
[dataImage writeToFile:filePath atomically:YES];

imageurl 打印如下

file://localhost/Users/varmab/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/E2EDF729-4684-465B-8BE3-2C0ACFA2EC03/Documents/2013-03-18%2012:02:31 %20+0000.jpg

现在,我要显示保存的图像,即图像存储在上述文件路径中

我尝试了以下代码:

//1st process:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageurl]];

//2nd process:
NSURL *urlString =imageurl;
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation([UIImage imageWithData:[NSData dataWithContentsOfURL:urlString]])];
UIImage *image = [UIImage imageWithData:imageData];

//3rd process:
NSData * imageData = [[NSData alloc] initWithContentsOfURL:imageurl ];
self.testingImageView.image = [UIImage imageWithData: imageData];

请指导我,我做错了什么吗?

【问题讨论】:

  • 使用 NSFileManager
  • 我确实喜欢这个但是,我得到了 null------- NSString * path = [[NSBundle mainBundle] pathForResource:@"images" ofType:@"jpeg"]; imgUlr=[NSURL URLWithString:path]; NSLog(@"********* testingImg is %@",imgUlr); @Rushabh

标签: ios objective-c xcode uiimageview uiimage


【解决方案1】:

为什么不直接用这个方法:

 - (id)initWithContentsOfFile:(NSString *)path

它已经在UIImage 类中可用。

[self.testingImageView setImage: [[UIImage alloc] initWithContentsOfURL:imageurl]];

试试这个,如果你再次遇到同样的问题,请告诉我。

注意:检查图像 url 是否与保存该图像时获得的旧图像 url 相似。

【讨论】:

  • 我得到的不是 initWithContentsOfURL ,而是 initWithContentsOfFile
  • 为什么我提到这个方法,因为它会减少你的代码行数。如果您对此感到满意,那么您能否接受并投票赞成答案?
【解决方案2】:

这将为您提供文档目录下所有文件的路径;

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

        NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];

        NSLog(@"files array %@", filePathsArray);

您可以使用以下代码获取filePathsArray中每个对象的路径;

    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:0]]; 

【讨论】:

    【解决方案3】:

    斯威夫特
    获取文档目录的路径,将图片存储在同一位置,您可以检索。

    func getDocumentsDirectory() -> URL {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        let documentsDirectory = paths[0]
        return documentsDirectory
    }
    
    if let image = UIImage(named: "example.png") {
        if let data = UIImagePNGRepresentation() {
            let filename = getDocumentsDirectory().appendingPathComponent("copy.png")
            try? data.write(to: filename)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-13
      • 2016-09-12
      相关资源
      最近更新 更多