加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 如何保存图像到应用程序tmp目录

发布时间:2020-12-15 01:54:27 所属栏目:百科 来源:网络整理
导读:HI为什么如果我使用NSTemporaryDirectory()将我的图像保存到xcode中,以便保存图像 /var/folders/oG/oGrLHcAUEQubd3CBTs-1zU+++TI/-Tmp-/ 而不是进入 /Users/ MyMac /Library/Application Support/iPhone Simulator/4.3.2/Applications/A685734E-36E9-45DD-B
HI为什么如果我使用NSTemporaryDirectory()将我的图像保存到xcode中,以便保存图像

/var/folders/oG/oGrLHcAUEQubd3CBTs-1zU+++TI/-Tmp-/

而不是进入

/Users/MyMac/Library/Application Support/iPhone Simulator/4.3.2/Applications/A685734E-36E9-45DD-BBE7-0A46F8F91DAF/tmp

这是我的代码:

-(NSString *)tempPath
{
    return NSTemporaryDirectory();
}

-(void) saveMyFoto
{
    NSString *urlNahledu = [NSString stringWithFormat:@"%@%@%@",@"http://www.czechmat.cz",urlFotky,@"_100x100.jpg"];
    NSLog(@"%@",urlNahledu);


    UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlNahledu]]];

    NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(image,0.8f)];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

    NSLog(@"%@    %@",paths,[self tempPath]);

    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"pkm.jpg"];

    [data writeToFile:localFilePath atomically:YES];

解决方法

这是使用URL直接获取到tmp目录的链接,然后返回该目录的文件URL(pkm.jpg)的首选方法:

Swift 3.0

let tmpURL = try! URL(fileURLWithPath: NSTemporaryDirectory(),isDirectory: true)
                    .appendingPathComponent("pkm")
                    .appendingPathExtension("jpg")
print("Filepath: (tmpURL)")

请注意,罕见但可能出现的错误未被处理。

Swift 2.0

let tmpDirURL = NSURL.fileURLWithPath(NSTemporaryDirectory(),isDirectory: true)
let fileURL = tmpDirURL.URLByAppendingPathComponent("pkm").URLByAppendingPathExtension("jpg")
print("FilePath: (fileURL.path)")

Objective-C的

NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:@"pkm"] URLByAppendingPathExtension:@"jpg"];
NSLog(@"fileURL: %@",[fileURL path]);

请注意,某些方法仍然要求路径作为字符串,然后使用[fileURL路径]将路径返回为字符串(如上所示在NSLog中)。
当升级目前的应用程序文件夹中的所有文件:

<Application_Home>/Documents/
<Application_Home>/Library/

保证从旧版本(< Application_Home> / Library / Caches子目录除外)保留。使用文档文件夹,您可能希望用户访问的文件和应用程序使用的文件的库文件夹,用户不应该看到。

另一种更长的方法可能是通过首先获取Document目录并剥离最后一个路径组件,然后添加tmp文件夹来获取tmp目录的URL:

NSURL *documentDir = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
NSURL *tmpDir = [[documentDir URLByDeletingLastPathComponent] URLByAppendingPathComponent:@"tmp" isDirectory:YES];
NSLog(@"tmpDir: %@",[tmpDir path]);

然后我们可以在那里找到一个文件,即pkm.jpg,如下所示:

NSString *fileName = @"pkm";
NSURL *fileURL = [tmpDir URLByAppendingPathComponent:fileName isDirectory:NO];
fileURL = [fileURL URLByAppendingPathExtension:@"jpg"];

字符串也可以通过旧的iOS系统使用,但上面的第一个URL方法是现在推荐的(除非你在写老系统:iPhone OS 2或3):

NSString *tmpDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,YES) firstObject];
tmpDir = [[tmpDir stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"tmp"];
NSString *filePath = [[tmpDir stringByAppendingPathComponent:@"pkm"] stringByAppendingPathExtension:@"jpg"];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读