我试图从webURL下载图像并将其存储到文档目录。
这是我的代码。下载的图像在文档目录中有零字节
-(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的图像是正确的我也可以从网页浏览器检查。 我不知道我的代码在哪里出错。
你能帮我解决吗?
在此先感谢。