我想将多个图像保存到我的文档路径文件夹中。我的Web服务返回多个图像url路径,我想将每个图像保存到我的文档文件夹。这个过程成功运行,但此时我的视图FREEZES(将图像url转换为NSData)。我正在使用以下代码。如何避免这个问题?
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[usersArray objectAtIndex:i]objectForKey:@"message"]]];
NSString *localImgpath = [directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",imageName, @"png"]];
[data writeToFile:localImgpath atomically:YES];
答案 0 :(得分:1)
您可能希望在后台线程中处理这个:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[usersArray objectAtIndex:i]objectForKey:@"message"]]];
NSString *localImgpath = [directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",imageName, @"png"]];
[data writeToFile:localImgpath atomically:YES];
});