0

我有一個tableview,它運行一些代碼,用於在後臺線程中從NSData獲取UIImage,然後在前臺調用[button setImage:image forState:UIControlStateNormal];setImage:forState:落後UI

問題是它在該行凍結了一秒鐘的UI,直到它完成。這意味着UI設置爲圖像時凍結。因爲用戶正在滾動,所以這是非常明顯的和緊張的。有沒有辦法避免我的用戶界面像這樣凍結?

dispatch_queue_t cellSetupQueue = dispatch_queue_create("Setup", NULL); 
dispatch_async(cellSetupQueue, ^{ 
    UIImage *image = [self.mediaDelegate largeThumbnailForMediaAtIndex:indexPath.row]; 
    dispatch_async(dispatch_get_main_queue(), ^{  
     [self.thumbnailButton setImage:image forState:UIControlStateNormal]; 
    }); 
}); 

dispatch_release(cellSetupQueue); 
+0

你可以發佈代碼嗎? – danh

+0

@danh將它貼在 – Andrew

+0

以上,我們需要'setThumbnailToImage:animated:'as-well – vikingosegundo

回答

3

我猜你的圖片是JPG。如果是這樣,那麼UIImage將推遲解碼實際圖像,直到它第一次實際需要這些像素。查看ImageIO框架以獲得更精確的控制。以下是我使用的一個片段:

CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); // data is your image file NSData 
if (source) 
{ 
    NSDictionary *dict = @{ (__bridge NSString *)kCGImageSourceShouldCache : @(YES) }; 
    CGImageRef cgImage = CGImageSourceCreateImageAtIndex(source, 0, (__bridge CFDictionaryRef)dict); 
    if (cgImage) 
    { 
     CFRelease(source); 
     return cgImage; // you can keep this reference so you dont have to decode again later 
    } 
} 

重要的部分是kCGImageSourceShouldCache選項。您可以在後臺線程上運行此代碼,並將解碼後的圖像傳遞給UI線程。

+0

這很好,謝謝。因此,如果'[self.mediaDelegate largeThumbnailForMediaAtIndex:indexPath.row];'方法通過了UIImage,我該如何將UIImage工作到上面的代碼中? – Andrew

+0

如果更改'largeThumbnailForMediaAtIndex:'從文件加載圖像,然後從CGImageSourceCreateImageAtIndex返回的CGImageRef創建UIImage,會更好。 –

+0

我使用了這段代碼,並且從它得到的圖像上做了'[UIImage imageWithCGImage:];',但它仍然將圖像加載到主線程的UI中。我做錯了嗎? – Andrew