为什么如果我NSTemporaryDirectory用来保存我的图像,图像就会保存到
在/ var /文件夹/ 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];
}
Run Code Online (Sandbox Code Playgroud)
Han*_*son 50
这是首选方法,它使用URL来直接获取tmp目录的链接,然后返回该目录的文件URL(pkm.jpg):
Swift 4.1
let url = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent("pkm", isDirectory: false)
.appendingPathExtension("jpg")
// Then write to disk
if let data = UIImageJPEGRepresentation(image, 0.8) {
do {
try data.write(to: url)
} catch {
print("Handle the error, i.e. disk can be full")
}
}
Run Code Online (Sandbox Code Playgroud)
Swift 3.1
let tmpURL = try! URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent("pkm")
.appendingPathExtension("jpg")
print("Filepath: \(tmpURL)")
Run Code Online (Sandbox Code Playgroud)
请注意,未处理可能的错误!
Swift 2.0
let tmpDirURL = NSURL.fileURLWithPath(NSTemporaryDirectory(), isDirectory: true)
let fileURL = tmpDirURL.URLByAppendingPathComponent("pkm").URLByAppendingPathExtension("jpg")
print("FilePath: \(fileURL.path)")
Run Code Online (Sandbox Code Playgroud)
Objective-C的
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:@"pkm"] URLByAppendingPathExtension:@"jpg"];
NSLog(@"fileURL: %@", [fileURL path]);
Run Code Online (Sandbox Code Playgroud)
请注意,某些方法仍然以字符串形式请求路径,然后使用它将路径作为字符串[fileURL path]返回(如上面NSLog中所示).升级当前App时文件夹中的所有文件:
<Application_Home>/Documents/
<Application_Home>/Library/
Run Code Online (Sandbox Code Playgroud)
保证保留旧版本(不包括<Application_Home>/Library/Caches子目录).将Documents文件夹用于您可能希望用户有权访问Library的文件,以及该应用程序使用且用户不应看到的文件的文件夹.
另一个更长的方法可能是获取tmp目录的url,首先获取Document目录并剥离最后一个路径组件,然后添加tmp文件夹:
NSURL *documentDir = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
NSURL *tmpDir = [[documentDir URLByDeletingLastPathComponent] URLByAppendingPathComponent:@"tmp" isDirectory:YES];
NSLog(@"tmpDir: %@", [tmpDir path]);
Run Code Online (Sandbox Code Playgroud)
然后我们可以在那里找到一个文件,即pkm.jpg,如下所示:
NSString *fileName = @"pkm";
NSURL *fileURL = [tmpDir URLByAppendingPathComponent:fileName isDirectory:NO];
fileURL = [fileURL URLByAppendingPathExtension:@"jpg"];
Run Code Online (Sandbox Code Playgroud)
使用旧的iOS系统上的字符串可以实现相同的功能,但现在推荐使用上面的第一个URL方法(除非您要写入旧系统:iPhone OS 2或3):
NSString *tmpDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
tmpDir = [[tmpDir stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"tmp"];
NSString *filePath = [[tmpDir stringByAppendingPathComponent:@"pkm"] stringByAppendingPathExtension:@"jpg"];
Run Code Online (Sandbox Code Playgroud)
因为在模拟器中运行的应用程序并不像在 iOS 设备中那样真正沙箱化。在模拟器上,Mac OS 的临时目录在 NSTemporaryDirectory() 中返回 - 在 iOS 设备上,临时目录实际上位于沙箱内。作为应用程序开发人员,您不应担心这种差异。
| 归档时间: |
|
| 查看次数: |
24715 次 |
| 最近记录: |