1

このコードを使用して、画像をドキュメントディレクトリに保存しようとしています。

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

4 に答える 4

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");}
于 2012-03-30T10:42:23.490 に答える
0

最初にファイルをアトミックに書き込み、次に次のことを試してください。

self.receiptImage1 = [UIImage imageWithContentsOfFile:fullPathToFile];
self.receiptImageView1 = [UIImageView initWithImage:self.receiptImage1];
于 2012-03-30T11:00:26.953 に答える
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];
    }
于 2012-03-30T12:35:37.983 に答える
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]];
于 2012-03-30T13:45:55.440 に答える