将数据保存到缓存目录(iPhone)时出错 [英] Error saving data to cache directory (iPhone)

查看:218
本文介绍了将数据保存到缓存目录(iPhone)时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下代码保存图片:

   - (void)writeData {

if(cacheFileName == nil)
return;
if(cacheDirectoryPath == nil)
return;


if(![[NSFileManager defaultManager] fileExistsAtPath:[self filePath]]){

NSData * imageData = UIImageJPEGRepresentation(self.image,0.9);

NSError * writeError = nil;

BOOL didWrite = [imageData writeToFile:[self filePath] options:NSAtomicWrite error:& writeError];

if(writeError)
NSLog([writeError localizedDescription]);

if(didWrite)
NSLog(@image saved);
else
NSLog(@image not saved);


}
}





- (NSString *)filePath {

NSArray * paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
NSString * cacheDirectory = [paths objectAtIndex:0];
NSString * filename = [cacheDirectory stringByAppendingPathComponent:cacheDirectoryPath];
filename = [filename stringByAppendingPathComponent:cacheFileName];
NSLog(filename);

return filename;

}

我收到错误:

  2009-06-23 16:39:19.740 XXX [33454:20b]操作无法完成。 (可能错误4.)



我看了看这个错误有以下含义: p>

  NSFileNoSuchFileError = 4,//尝试对不存在的文件执行文件系统操作



这不合理,因为文件不应该存在。



已尝试编写png表示并使用方便方法:

   - (BOOL)writeToFile:(NSString *)path atomically :( BOOL)flag 



我使用类似的代码保存plist,他们工作正常。我不知道什么问题是。任何想法?

解决方案

我看到你在缓存目录中使用一些中间目录,是/他们创建?如果不尝试删除它们或使用

   -  [NSFileManager createDirectoryAtPath:withIntermediateDirectories:attributes:error:] 

以在

之前创建

I am trying to save images using the following code:

- (void)writeData{

    if(cacheFileName==nil)
        return;
    if(cacheDirectoryPath==nil)
        return;


    if (![[NSFileManager defaultManager] fileExistsAtPath:[self filePath]]) {        

        NSData *imageData = UIImageJPEGRepresentation(self.image, 0.9);

        NSError *writeError= nil;

        BOOL didWrite =  [imageData writeToFile:[self filePath] options:NSAtomicWrite error:&writeError];

        if(writeError)
            NSLog([writeError localizedDescription]);

        if(didWrite)
            NSLog(@"image saved");
        else
            NSLog(@"image not saved");


    }
}





 - (NSString *)filePath{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
    NSString *cacheDirectory = [paths objectAtIndex:0]; 
    NSString *filename = [cacheDirectory stringByAppendingPathComponent:cacheDirectoryPath]; 
    filename = [filename stringByAppendingPathComponent:cacheFileName]; 
    NSLog(filename);

    return filename;

}

I get an error:

2009-06-23 16:39:19.740 XXX[33454:20b] Operation could not be completed. (Cocoa error 4.)

I looked this up and see the error has the following meaning:

 NSFileNoSuchFileError = 4,                 // Attempt to do a file system operation on a non-existent file

Which doesn't make sense since the file shouldn't exist.

I have tried writing the png representation and using the convenience method:

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag

I am using similar code to save plists, and they work fine. I am not sure what the problem is. Any ideas?

解决方案

I see that you use some intermediate directory within the Cache Directory, are it/they created? If not try removing them or use

-[NSFileManager createDirectoryAtPath:withIntermediateDirectories:attributes:error:]

to create them before

这篇关于将数据保存到缓存目录(iPhone)时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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