1

したがって、次のような URL から画像を取得する関数があります。

- (UIImage *) getImageFromURL:(NSString *)fileURL {
    UIImage * result;

    NSData * img_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    result = [UIImage imageWithData:img_data];

    return result;
}

次に、次のような UIImage から画像を保存する関数:

    -(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];
        NSLog(@"Image named %@ wrote to %@", imageName, directoryPath);
    } else {
        NSLog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
    }
}

私が直面している問題は、saveImage 関数を使用して UIImage を実行すると、画像が保存されているとは思えないことです。これは、UIImage imageNamed:@"Documents/filenamesavedas.jpeg" をチェックすると (null) が返されるためです。ただし、画像は正しくダウンロードされます。

これが私のドキュメントディレクトリパス関数です:

- (NSString *)getDocumentsDirectoryPath {
    NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSLog(documentsDirectoryPath);
    return documentsDirectoryPath;
}

画像をダウンロードして保存しようとしている場所は次のとおりです。

   dispatch_async(jsonQueue, ^{
        // check to see if data authenticate runs successfully. 
    NSString *documentsDirectoryPath = [util getDocumentsDirectoryPath];
    UIImage *imageToSave = [util getImageFromURL:[properties objectForKey:@"current_group_logoURL"]];
    [util saveImage:imageToSave withFileName:[properties objectForKey:@"home_venue_logo"] ofType:@"jpeg" inDirectory:documentsDirectoryPath];
    NSLog(@"Group Logo URL %@", [properties objectForKey:@"current_group_logoURL"]);
    NSLog(@"Current Group Image %@", [properties objectForKey:@"home_venue_logo"]);        
        UIImage *testToSeeItHasDownloaded = imageToSave;
        NSString *imageLocation = [documentsDirectoryPath stringByAppendingPathComponent:[properties objectForKey:@"home_venue_logo"]];
        NSData *data = [NSData dataWithContentsOfFile:imageLocation];
        UIImage *testToSeeImageHasSaved = [UIImage imageWithData:data];
        NSLog(@"testToSeeImagehasDownloaded: %@", testToSeeItHasDownloaded);
        NSLog(@"image location to save to: %@", imageLocation);
        NSLog(@"testToSeeImagehasSaved: %@", testToSeeImageHasSaved );    

    });

コンソールはこれを返します:

2012-10-22 16:27:45.642 asdaf[46819:1d807] Group Logo URL http://somesite.com/50a0e42619424ba551a956511c098656.jpeg
2012-10-22 16:27:45.642 asdaf[46819:1d807] Current Group Image 50a0e42619424ba551a956511c098656.jpeg
2012-10-22 16:27:45.649 asdaf[46819:1d807] testToSeeImagehasDownloaded: <UIImage: 0xb09ed50>
2012-10-22 16:27:45.649 asdaf[46819:1d807] testToSeeImagehasSaved: (null)

testToSeeImagehasSaved は null を返す必要があります。しかし、私は何を間違っていますか?ありがとう。

4

4 に答える 4

1
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//documentsDirectory is your documents directory path

//Now append your image name in this path
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"ImageName.jpeg"];

//convert your image in NSData
NSData *imageData = UIImagePNGRepresentation(image);
//Now write it at path
[imageData writeToFile:imagePath atomically:NO];

この画像も同じ方法で取得できます

NSData *data = [NSData dataWithContentsOfFile:imagePath];
UIImage *image = [UIImage imageWithData:data];

編集:

-(void)saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension 
{
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *imageName = [imageName stringByAppendingString:extension];
   //Note extension should be like .png, .jpg, .jpeg dont forget dot (.).

   NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:imageName];

   NSData *imageData = UIImagePNGRepresentation(image);
   [imageData writeToFile:imagePath atomically:NO]; 
}

コード全体については今はわかりませんがUIImage、画像をドキュメントディレクトリに保存する必要があるときにオブジェクトを渡すのはなぜですか。メソッドで渡すこともできNSDataます。あなたはもっとやっています。

あなたの問題は、毎回 .jpg 画像を保存していて、取得時に同じパスで .jpeg 画像を見つけようとしているため、null 値を取得していることです。保存方法でこの行を参照してください-

[UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
于 2012-10-22T15:47:17.490 に答える
0
[UIImage imageNamed:@"Documents/filenamesavedas.jpeg"]

パスを使用せず、バンドルで特定の名前のファイルを検索します。

NSFileManager への呼び出しを使用してファイルを見つけ、予想されるパスとファイル名を使用します。

于 2012-10-22T17:29:33.320 に答える
0

この行は私には正しく見えません:

UIImage *testToSeeImageHasSaved = [UIImage imageNamed: [@"Documents/" stringByAppendingString: [properties objectForKey:@"home_venue_logo"]]];

私はあなたがしなければならないと思います:

UIImage* image = [UIImage imageWithContentsOfFile:"YOUR_FILE_PATH"];

[UIImage imageNamed:"image_name"]プロジェクトに添付された画像にのみ適しています

于 2012-10-22T15:45:26.220 に答える
0

ドキュメントフォルダに保存すると、「ドキュメント」ディレクトリに正確に保存されるのではなく、

path=/var/mobile/Applications/144B3628-5258-4939-8B80-006EC5BE45B2/Library/Caches/6_1.png

したがって、画像を取得するには、使用する必要があります

NSString *uniquePath = [directory stringByAppendingPathComponent: filename];
image = [UIImage imageWithContentsOfFile: uniquePath];

directory は保存に使用したパス、filename は jpeg の名前です。

于 2012-10-22T15:48:04.170 に答える