如何将图像保存到我的应用程序的tmp目录? [英] How can I save an image to my app's tmp dir?

查看:109
本文介绍了如何将图像保存到我的应用程序的tmp目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么如果我使用 NSTemporaryDirectory 来保存我的图像,图像会保存到

Why is that if I use NSTemporaryDirectory to save my image, the image is saved into


/ var / folders / oG / oGrLHcAUEQubd3CBTs-1zU +++ TI / -Tmp - /

/var/folders/oG/oGrLHcAUEQubd3CBTs-1zU+++TI/-Tmp-/

而不是


/ 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

这是我的代码:

-(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];
}


推荐答案

这是首选的方法使用URL来直接获取tmp目录的链接,然后返回该目录的文件URL(pkm.jpg):

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 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

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

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中所示)。
升级当前应用程序文件夹中的所有文件时:

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:

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

保证保留旧版本(不包括< Application_Home> / Library / Caches 子目录)。对于您可能希望用户有权访问的文件,使用 Documents 文件夹,对于App的文件,使用 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.

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

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:

NSURL *documentDir = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
NSURL *tmpDir = [[documentDir URLByDeletingLastPathComponent] URLByAppendingPathComponent:@"tmp" isDirectory:YES];
NSLog(@"tmpDir: %@", [tmpDir path]);

然后我们可以在那里找到一个文件,即pkm.jpg,如下所示:

Then we can address a file there, i.e. pkm.jpg as shown here:

NSString *fileName = @"pkm";
NSURL *fileURL = [tmpDir URLByAppendingPathComponent:fileName isDirectory:NO];
fileURL = [fileURL URLByAppendingPathExtension:@"jpg"];






同样可以用字符串完成,这是在旧的iOS系统上使用的方式,但上面的第一个URL方法是现在推荐的方法(除非您写入旧系统:iPhone OS 2或3):


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):

NSString *tmpDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
tmpDir = [[tmpDir stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"tmp"];
NSString *filePath = [[tmpDir stringByAppendingPathComponent:@"pkm"] stringByAppendingPathExtension:@"jpg"];

这篇关于如何将图像保存到我的应用程序的tmp目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆