所以我已經經歷了幾乎所有關於SO的問題,我已經把它們放在一起來創建一個接受兩個參數的方法,首先是要下載並顯示在UIImageView
中的圖像的URL
其次是那個UIImageView
的佔位符圖像。我想保存圖像,以便它不會每次都下載。我已經使用SDWebImage來下載圖像,但是在使用SDWebImage將圖像保存到文檔目錄時,我有些困惑,所以我決定不使用它。我用dispatch_sync(dispatch_get_main_queue()
,我的方法現在看起來像:異步保存圖像
- (void)saveImageFromURL:(UIImage*)image:(NSURL*)imageURL {
NSString *url = [imageURL absoluteString];
NSArray *parts = [url componentsSeparatedByString:@"/"];
NSString *filename = [parts lastObject];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", filename]];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath];
if (fileExists) {
NSLog(@"File already exists");
_myUIImage.image = [UIImage imageNamed:fullPath];
return;
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
[self.myUIImage sd_setImageWithURL:imageURL placeholderImage:image];
UIImage *imageFromURL = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
NSData *imageDataNew = UIImagePNGRepresentation(imageFromURL);
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:fullPath contents:imageDataNew attributes:nil];
});
}
}
我有幾個問題,這是落實不夠好,因爲我是在一個應用程序,會在應用程序商店的工作?從URL下載圖像是否會異步完成? (我知道我使用dispatch_async
,但只需要確認)。如果是的話,那麼這不會阻止我的用戶界面,對吧?
很難。因爲您在dispatch_get_main_queue中執行此操作 – heximal
您的意思是輸入:dispatch_sync(dispatch_get_main_queue()'''沒有最後一個括號,或者是一個拼寫錯誤嗎? –
dispatch_sync - 正如您可以期望的那樣 - 它會這意味着如果你從主線程調用saveImageFromURL - 它將被阻塞。更多dispatch_get_main_queue() - 實際上是一個主線程。 因此,你的實現將阻止UI。 – DoN1cK