2014-12-04 87 views
0

我想將多個圖像保存到我的文檔路徑文件夾中。我的web服務返回多個圖像url路徑,並且我想將每個圖像保存到我的文檔文件夾中。此過程成功運行,但此時我的視圖凍結(將圖像url轉換爲NSData)。我正在使用以下代碼。如何避免這個問題?將多個NSData文件保存到iOS中的文檔文件夾中

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

你不滿意的解決方案? – user623396 2014-12-15 14:17:07

回答

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]; 
}); 
相關問題