Iphone 如何将图像保存到我的应用程序&x27;谁是tmp总监?
为什么使用Iphone 如何将图像保存到我的应用程序&x27;谁是tmp总监?,iphone,xcode,tmp,Iphone,Xcode,Tmp,为什么使用NSTemporaryDirectory保存图像时,图像会保存到 /var/folders/oG/ogrlhcaueqbd3cbts-1zU++TI/-Tmp-/ 而不是 /用户/MyMac/Library/Application Support/iPhone Simulator/4.3.2/Applications/A685734E-36E9-45DD-BBE7-0A46F8F91DAF/tmp 这是我的密码: -(NSString *)tempPath { return N
NSTemporaryDirectory
保存图像时,图像会保存到
/var/folders/oG/ogrlhcaueqbd3cbts-1zU++TI/-Tmp-/
而不是
/用户/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];
}
因为在模拟器中运行的应用程序并不像在iOS设备中那样是沙盒。在模拟器上,来自Mac OS的临时目录在NSTemporaryDirectory()中返回-在iOS设备上,临时目录实际上位于沙箱中。
作为一名应用程序开发人员,这种差异不应该让你担心。如果你想将图像存储在/Users/MyMac/Library/Application Support/iPhone Simulator/4.3.2/Applications/A685734E-36E9-45DD-BBE7-0A46F8F91DAF/tmp中 然后使用nshomeditory(),它将您的位置设置为/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"];
这是首选方法,它使用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")
}
}
let tmpURL = try! URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent("pkm")
.appendingPathExtension("jpg")
print("Filepath: \(tmpURL)")
Swift 3.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")
}
}
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)")
目标-C
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:@"pkm"] URLByAppendingPathExtension:@"jpg"];
NSLog(@"fileURL: %@", [fileURL path]);
请注意,有些方法仍然以字符串形式请求路径,然后使用[fileURL path]
以字符串形式返回路径(如上NSLog所示)。
升级当前应用程序时,文件夹中的所有文件:
<Application_Home>/Documents/
<Application_Home>/Library/
然后我们可以在那里寻址一个文件,即pkm.jpg,如下所示:
NSString *fileName = @"pkm";
NSURL *fileURL = [tmpDir URLByAppendingPathComponent:fileName isDirectory:NO];
fileURL = [fileURL URLByAppendingPathExtension:@"jpg"];
字符串也可以实现这一点,以前在旧的iOS系统上使用字符串,但现在推荐使用上面的第一个URL方法(除非您是在旧的系统上编写:iPhone OS 2或3): Swift 5版本的用户答案
let url = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent("pkm", isDirectory: false)
.appendingPathExtension("jpg")
// Then write to disk
if let data = image.jpegData(compressionQuality: 0.8) {
do {
try data.write(to: url)
} catch {
print("Handle the error, i.e. disk can be full")
}
}
这不起作用,文本谈到NSHomeDirectory(),代码使用NSDocumentDirectory,因此,/tmp目录将是错误的Documents目录的一部分。这篇文章解释了iOS应用程序的所有目录,save,delete
uiImageJPEGresentation()
已经返回了NSData*
-为什么在使用它之前要将它传递给dataWithData:
?我想知道你是否知道一些我不知道的事情…我无法从NSTemporaryDirectory()
获取文件,什么时候是视频文件.mp4。适用于音频和图像。Help@jose920405我不太理解您的问题,但这可能与文件是否受iOS支持有关。isDirectory
很重要