xcode 如何将图像保存到我的应用程序的 tmp 目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7444982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I save an image to my app's tmp dir?
提问by vosy
Why is that if I use NSTemporaryDirectory
to save my image, the image is saved into
为什么如果我用NSTemporaryDirectory
保存我的图像,图像被保存到
/var/folders/oG/oGrLHcAUEQubd3CBTs-1zU+++TI/-Tmp-/
/var/folders/oG/oGrLHcAUEQubd3CBTs-1zU+++TI/-Tmp-/
and not into
而不是进入
/Users/MyMac/Library/Application Support/iPhone Simulator/4.3.2/Applications/A685734E-36E9-45DD-BBE7-0A46F8F91DAF/tmp
/Users/ MyMac/Library/Application Support/iPhone Simulator/4.3.2/Applications/A685734E-36E9-45DD-BBE7-0A46F8F91DAF/tmp
Here is my code:
这是我的代码:
-(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];
}
回答by Sverrisson
This is the preferred method which uses URL's to get a link directly to the tmp directory and then returns a file URL (pkm.jpg) for that directory:
这是首选方法,它使用 URL 直接获取到 tmp 目录的链接,然后返回该目录的文件 URL (pkm.jpg):
Swift 4.1
斯威夫特 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")
}
}
Swift 3.1
斯威夫特 3.1
let tmpURL = try! URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent("pkm")
.appendingPathExtension("jpg")
print("Filepath: \(tmpURL)")
Note that a possible error is not handled!
请注意,不会处理可能的错误!
Swift 2.0
斯威夫特 2.0
let tmpDirURL = NSURL.fileURLWithPath(NSTemporaryDirectory(), isDirectory: true)
let fileURL = tmpDirURL.URLByAppendingPathComponent("pkm").URLByAppendingPathExtension("jpg")
print("FilePath: \(fileURL.path)")
Objective-C
目标-C
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:@"pkm"] URLByAppendingPathExtension:@"jpg"];
NSLog(@"fileURL: %@", [fileURL path]);
Note that some methods still request a path as string, then use the [fileURL path]
to return the path as string (as shown above in the NSLog).
When upgrading a current App all files in the folders:
请注意,某些方法仍然以字符串形式请求路径,然后使用[fileURL path]
将路径作为字符串返回(如上图 NSLog 中所示)。升级当前应用程序文件夹中的所有文件时:
<Application_Home>/Documents/
<Application_Home>/Library/
are guaranteed to be preserved from the old version (excluding the <Application_Home>/Library/Caches
subdirectory). Use the Documents
folder for files you may want the user to have access to and the Library
folder for files that the App uses and the User should not see.
保证保留旧版本(不包括<Application_Home>/Library/Caches
子目录)。使用Documents
您可能希望用户有权访问的Library
文件的文件夹以及应用程序使用且用户不应看到的文件的文件夹。
Another longer waymight be to get an url to the tmp directory, by first getting the Document directory and stripping the last path component and then adding the tmp folder:
另一种更长的方法可能是获取 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]);
Then we can address a file there, i.e. pkm.jpg as shown here:
然后我们可以在那里找到一个文件,即 pkm.jpg,如下所示:
NSString *fileName = @"pkm";
NSURL *fileURL = [tmpDir URLByAppendingPathComponent:fileName isDirectory:NO];
fileURL = [fileURL URLByAppendingPathExtension:@"jpg"];
The same may be accomplished with strings, which was used by the way on older iOS systems, but the first URL method above is the recommended one now (unless you are writing to older systems: iPhone OS 2 or 3):
使用字符串也可以实现相同的效果,在较旧的 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"];
回答by Felix
Because apps running in the simulator are not really sandboxed like in iOS devices. On the simulator a temporary directory from Mac OS is returned in NSTemporaryDirectory() - on an iOS device the temporary directory is really within the sandbox. That difference shouldn't concern you as an app developer.
因为在模拟器中运行的应用程序并不像在 iOS 设备中那样真正被沙箱化。在模拟器上,NSTemporaryDirectory() 中返回 Mac OS 的临时目录 - 在 iOS 设备上,临时目录实际上位于沙箱中。作为应用程序开发人员,您不应该担心这种差异。
回答by sandy
If you want to store your images in /Users/MyMac/Library/Application Support/iPhone Simulator/4.3.2/Applications/A685734E-36E9-45DD-BBE7-0A46F8F91DAF/tmp
如果要将图像存储在 /Users/MyMac/Library/Application Support/iPhone Simulator/4.3.2/Applications/A685734E-36E9-45DD-BBE7-0A46F8F91DAF/tmp
then use nshomedirectory() which give you location upto /Users/MyMac/Library/Application Support/iPhone Simulator/4.3.2/Applications/A685734E-36E9-45DD-BBE7-0A46F8F91DAF then you just put /tmp and store your images.
然后使用 nshomedirectory() 为您提供位置至 /Users/MyMac/Library/Application Support/iPhone Simulator/4.3.2/Applications/A685734E-36E9-45DD-BBE7-0A46F8F91DAF 然后您只需放置 /tmp 并存储您的图像。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString* docDir = [paths objectAtIndex:0];
NSString* file = [docDir stringByAppendingString:@"/tmp"];