如何将图像保存到应用程序的临时目录?
为什么如果我使用 NSTemporaryDirectory 保存图像,图像会保存到
/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];
}
Why is that if I use NSTemporaryDirectory
to save my image, the image is saved into
/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
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是首选方法,它使用 URL 直接获取 tmp 目录的链接,然后返回该目录的文件 URL (pkm.jpg):
Swift 5+
Swift 4.1
Swift 3.1
请注意,可能的错误不会被处理!
Swift 2.0
Objective-C
请注意,某些方法仍然请求字符串形式的路径,然后使用
[fileURL path]
以字符串形式返回路径(如上面 NSLog 中所示)。升级当前应用程序时,
保证保留旧版本文件夹中的所有文件(不包括
/Library/Caches
子目录)。使用Documents
文件夹存放您可能希望用户有权访问的文件,使用Library
文件夹存放应用程序使用但用户不应看到的文件。另一种更长的方法可能是获取 tmp 目录的 url,首先获取 Document 目录并删除最后一个路径组件,然后添加 tmp 文件夹:
然后我们可以在那里寻址一个文件,即 pkm .jpg 如下所示:
使用字符串也可以完成同样的操作,这在较旧的 iOS 系统上使用过,但现在推荐使用上面的第一种 URL 方法(除非您正在写入较旧的系统:iPhone OS 2 或 3) ):
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:
Swift 5+
Swift 4.1
Swift 3.1
Note that a possible error is not handled!
Swift 2.0
Objective-C
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:
are guaranteed to be preserved from the old version (excluding the
<Application_Home>/Library/Caches
subdirectory). Use theDocuments
folder for files you may want the user to have access to and theLibrary
folder for files that the App uses and the User should not see.Another longer way might 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:
Then we can address a file there, i.e. pkm.jpg as shown here:
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 设备中那样真正沙箱化。在模拟器上,Mac OS 的临时目录在 NSTemporaryDirectory() 中返回 - 在 iOS 设备上,临时目录实际上位于沙箱内。
作为应用程序开发人员,您不应担心这种差异。
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.
Sverrisson 答案的 Swift 5 版本。
Swift 5 version of Sverrisson 's answer.
如果您想将图像存储在 /Users/MyMac/Library/Application Support/iPhone Simulator/4.3.2/Applications/A685734E-36E9-45DD-BBE7-0A46F8F91DAF/tmp 中,
请使用 nshomedirectory() 为您提供到 /Users 的位置/MyMac/库/应用程序支持/iPhone Simulator/4.3.2/Applications/A685734E-36E9-45DD-BBE7-0A46F8F91DAF 然后你只需放入 /tmp 并存储你的图像。
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
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.