[英]Downloaded images has zero bytes in document directory
I am trying to download and store image from webURL to Document Directory. 我正在尝试将图像从webURL下载并存储到文档目录。
Here is my code for that. 这是我的代码。
-(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);
}
}
Every time "Avatar photo stored at : ..." is going to print in console. 每次将在控制台中打印“头像照片存储在... ...”时。 But if I go to that path and check image then it has zero bytes of size and no any preview available.
但是,如果我转到该路径并检查图像,则它的大小为零字节,并且没有可用的预览。
webURL of image is correct I can also check from web Browser. 图片的webURL正确,我也可以从网络浏览器检查。 I don't know where is mistake in my code.
我不知道代码中的错误在哪里。
Can you please help me to solve this?? 您能帮我解决这个问题吗?
Thanks in advance. 提前致谢。
dataWithContentsOfURL
is not download image asynchronously
from server. dataWithContentsOfURL
不是从服务器asynchronously
下载图像。 now if imagesize is big enough then it takes some time for downloading. 现在,如果imagesize足够大,则需要花费一些时间进行下载。 and you are directly trying to store image to document directory.
并且您直接尝试将图像存储到文档目录。 I think that create problem.
我认为这会造成问题。 you should use
NSUrlSession
for getting image and you should write data to local storage like documents directory from completion handler of NSUrlSession
method call. 您应该使用
NSUrlSession
来获取图像,并且应该从NSUrlSession
方法调用的完成处理程序将数据写入本地存储(如文档目录)。 you can use AFNetworking
also to manage this kind od stuff. 您还可以使用
AFNetworking
来管理此类od的东西。
Second thing use 第二件事使用
[data writeToFile:path atomically:NO];
to store data to document directory and path is final path and should be unique for different data. 将数据存储到文档目录,并且路径是最终路径,并且对于不同的数据应该是唯一的。
[NSData dataWithContentsOfURL]
is not a asynchronously method [NSData dataWithContentsOfURL]
不是异步方法
if you Debug you code you will find imagedata
will be nil 如果您调试代码,您将发现
imagedata
将为nil
you should first get imagedata then store it 您应该首先获取图像数据,然后将其存储
you can use AFNetworing
, SDWebImage
framework or just use NSURLSession
download it 您可以使用
AFNetworing
, SDWebImage
框架或仅使用NSURLSession
下载它
here's one solution of my own 这是我自己的解决方案
[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError * error) { //error if (error) { //handle error return; } if (data) { //store imagedata [data writeToFile:filepath atomically:NO]; } }];
good day :) 美好的一天 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.