webURLからDocument Directoryにイメージをダウンロードして保存しようとしています。
これは私のコードです。ダウンロードしたイメージのドキュメントディレクトリに0バイトが含まれています
-(void)downloadPersonPhoto:(NSDictionary *)dict
{
NSString *imageUrlStr=[dict objectForKey:@"personPhoto"];
if ([[dict valueForKey:@"personAvatarStore"] isEqualToString:@"AMAZON"])
{
imageUrlStr = [imageUrlStr stringByReplacingOccurrencesOfString:@"original" withString:@"100x100"];
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectory = [self getDocumentDirectory];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"personPhoto"];
if (![fileManager fileExistsAtPath:dataPath]) {
[fileManager createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; //Create folder
}
NSString *fullPath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",[dict valueForKey:@"personId"]]];
if (![fileManager fileExistsAtPath:fullPath]) {
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrlStr]];
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];
NSLog(@"Avatar Photo stored at: %@", fullPath);
}
}
「に格納されているアバターの写真が:...」するたびに、コンソールに印刷しようとしているが。しかし、私がそのパスに行き、画像をチェックすると、サイズはゼロバイトになり、プレビューはありません。
画像のwebURLが正しいwebブラウザからも確認できます。 私のコードでどこが間違っているのか分かりません。
これを解決するために私を助けてもらえますか?
ありがとうございます。