Iphone 目标c:无法将图像写入文档目录

Iphone 目标c:无法将图像写入文档目录,iphone,objective-c,ios,uiimageview,nsdocumentdirectory,Iphone,Objective C,Ios,Uiimageview,Nsdocumentdirectory,我正在使用这段代码尝试将图像保存到documents目录: //FOR TESTING ON SIMULATOR UIImage* image = [UIImage imageNamed:@"ReceiptImage1.jpg"]; NSData *imageData = UIImagePNGRepresentation(image); NSString* imageName = @"Receipt1Image1.png"; NSArray* paths = NSSearchPathForDi

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

//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图像设置为我试图写入documents目录的收据图像

有人能解释一下我做错了什么以及如何修复它吗

谢谢

杰克

用这个:

- (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");}
使用以下命令:

- (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");}

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

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

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

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

如果已经存在文件,则可能不会保存到该路径。(查看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];
    }

如果已经存在文件,它可能不会保存到该路径。(查看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];
    }

您可以采取以下方法

-(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];
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
yourImage.image = [self loadImage:@"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];
[self saveImage:yourImage withFileName:@"Your Image Name" ofType:@"png" inDirectory:path];
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]];

您可以采取以下方法

-(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];
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
yourImage.image = [self loadImage:@"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];
[self saveImage:yourImage withFileName:@"Your Image Name" ofType:@"png" inDirectory:path];
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]];

您是否检查了
writeToFile:atomically:
的返回值?如果有错误,您可以使用
writeToFile:options:error:
变量检查返回值。另外,为什么您实际上需要将图像从应用程序包复制到documents目录?是否检查了设备中的代码?因为模拟器不会执行代码逐行..[imageData WriteFile:fullPathToFile原子化:否];方法将图像写入路径需要一些时间,因此请在一段时间后尝试在图像中加载图像。您的代码工作正常。请签出iOutlet for receiptImageView1。@MrTJ一旦我成功地将图像保存到文档目录,我将更改代码以从相机/照片中拍摄图像,这仅用于在模拟器。谢谢大家。您是否检查了
writeToFile:atomicaly:
的返回值?如果有错误,您可以使用
writeToFile:options:error:
变量来检查它是什么。另外,为什么您实际上需要将图像从应用程序包复制到文档目录?是否检查了设备中的代码?因为模拟器或者不逐行执行代码..[imageData WriteFile:fullPathToFile原子性:否];方法将图像写入路径需要一些时间,因此请在一段时间后尝试在图像中加载图像。您的代码工作正常。请签出iOutlet for receiptImageView1。@MrTJ一旦我成功地将图像保存到文档目录,我将更改代码以从相机/照片中拍摄图像,这仅用于在谢谢大家。