我正在获得像这样的响应对象
responseObject = { " img":" images / ProfileImage / defaultImg.jpg", }
我必须将此图像存储在app文件夹中。有没有办法做到这一点。
答案 0 :(得分:0)
路径不完整
您需要在" images / ProfileImage / defaultImg.jpg"
之前附加基本网址然后
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *urlToDownload = BaseURL + "images/ProfileImage/defaultImg.jpg";
NSURL *url = [NSURL URLWithString:urlToDownload];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if (urlData)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"defaultImg.png"];
//saving is done on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[urlData writeToFile:filePath atomically:YES];
NSLog(@"File Saved !");
});
}
});
答案 1 :(得分:0)
以下是用于将图像下载并存储到iOS应用程序文件夹
中的本地“文档”目录的代码在下面的代码urlString
中是图片的网址。
dispatch_queue_t downloadThumbNail = dispatch_queue_create("com.download.thumbnail", NULL);
dispatch_async(downloadThumbNail, ^
{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
UIImage *imageThumb = [UIImage imageWithData:imageData];
dispatch_async(dispatch_get_main_queue(),
^{
NSString *str = @“filename.png”;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:str];
NSData *pngData = UIImagePNGRepresentation(imageThumb);
[pngData writeToFile:filePath atomically:YES];
});
});
享受编码。