iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – 如何将图像保存到我的应用程序的tmp目录?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

为什么如果我使用NSTemporaryDirectory保存我的图像,图像将保存到 /var/folders/oG/oGrLHcAUEQubd3CBTs-1zU+++TI/-Tmp-/ 而不是 /Users/Mymac/Library/Application Support/iPhone Simulator/4.3.2/Applications/A685734E-36E9-45DD-BBE7-0A
为什么如果我使用NstemporaryDirectory保存我的图像,图像将保存到

而不是

这是我的代码

-(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,@R_944_993@,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 4.0

let tmpURL = URL(fileURLWithPath: NstemporaryDirectory(),isDirectory: truE)
    .appendingPathComponent("pkm")
    .appendingPathExtension("jpg")
print("Filepath: \(tmpURL)")

// Then write to disk
if let url = tmpURL,let data = UIImageJPEGRepresentation(image,0.8) {
    // Use do-catch for error handling,disk can be full etc.
    try? data.write(to: url)
}

Swift 3.1

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 path]将路径作为字符串返回(如上面NSLog中所示)。
升级当前App时文件夹中的所有文件

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

保证保留旧版本(不包括< Application_Home> / Library / Caches子目录)。将Documents文件夹用于您可能希望用户有权访问的文件,并使用Library文件夹找到App使用且用户不应看到的文件

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

NSURL *documentDir = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:@R_944_993@] 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"];

大佬总结

以上是大佬教程为你收集整理的iphone – 如何将图像保存到我的应用程序的tmp目录?全部内容,希望文章能够帮你解决iphone – 如何将图像保存到我的应用程序的tmp目录?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。