objective-c:无法将图像写入文档目录

时间:2012-03-30 10:31:15

标签: iphone objective-c ios uiimageview nsdocumentdirectory

我正在使用这段代码尝试将图像保存到文档目录中:

//FOR TESTING ON SIMULATOR 
UIImage* image = [UIImage imageNamed:@"ReceiptImage1.jpg"];
NSData *imageData = UIImagePNGRepresentation(image);
NSString* imageName = @"Receipt1Image1.png";
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];        
[imageData writeToFile:fullPathToFile atomically:NO];

self.receiptImage1 = fullPathToFile;

self.receiptImageView1.image = [UIImage imageWithContentsOfFile:fullPathToFile];

NSLog(@"fullPathToFile %@", fullPathToFile);

但是,这段代码并没有将imageViews图像设置为我试图写入文档目录的收据图像。

有人可以解释我做错了什么以及如何解决这个问题?

谢谢,

杰克

4 个答案:

答案 0 :(得分:6)

使用此:

- (void)saveImage:(UIImage*)image:(NSString*)imageName{
NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.

NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it

NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory

NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path 

[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)

NSLog(@"image saved");}

答案 1 :(得分:0)

首先以原子方式写入文件,然后尝试:

self.receiptImage1 = [UIImage imageWithContentsOfFile:fullPathToFile];
self.receiptImageView1 = [UIImageView initWithImage:self.receiptImage1];

答案 2 :(得分:0)

如果已存在文件,则可能无法保存到该路径。 (检查API文档,我记不清楚。但要保存,你应该添加类似的东西:

    NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [dirs objectAtIndex:0];
    _exportPath = [documentsDirectoryPath stringByAppendingPathComponent:aFileName];  // may need to hang onto him

    if ([[NSFileManager defaultManager] fileExistsAtPath:_exportPath]) {
        [[NSFileManager defaultManager] removeItemAtPath:_exportPath
                                                   error:nil];
    }

答案 3 :(得分:0)

您可以采取以下方法

-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
    if ([[extension lowercaseString] isEqualToString:@"png"]) {
        [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
    } else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
        [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
    } else {
        ALog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
    }
}

要保存图像,只需按以下步骤操作:

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[self saveImage:yourImage withFileName:@"Your Image Name" ofType:@"png" inDirectory:path];

要加载图像,请实现此方法:

-(UIImage *) loadImage:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {

    UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.%@", directoryPath, fileName, extension]];

    return result;
}

然后像这样使用它:

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
yourImage.image = [self loadImage:@"Your Image Name" ofType:@"png" inDirectory:path];

或者您可以简单地将图像设置为等于此方法返回的内容而不是创建方法:

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
yourImage.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/Your Image Name.png", path]];