2016-02-19 32 views
-1

我在集合視圖中顯示了多張專輯圖像。 用於在單元格方法中顯示圖像cellForItemAtIndexPath包含如下代碼。如何使UICollectionView滾動更快

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 

     AlbumImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AlbumImageCell" forIndexPath:indexPath]; 
     cell.layer.shouldRasterize = YES; 
     cell.layer.rasterizationScale = [UIScreen mainScreen].scale; 
     if (cell == nil) { 
     cell = [[[NSBundle mainBundle] loadNibNamed:@"AlbumImageCell" owner:self options:nil] objectAtIndex:0]; 
     } 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *inputPath = [[self.arrTbl_Album_Image objectAtIndex:indexPath.row]objectAtIndex:2 ]; 

    NSString *imageEx=[inputPath pathExtension]; 
    NSString *imageName=[[inputPath lastPathComponent]stringByDeletingPathExtension]; 

    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",imageName,imageEx]]; 

    [cell.albumImage setImage:[UIImage imageWithContentsOfFile:fullPath]]; 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSData *data= [NSData dataWithContentsOfFile:fullPath]; 
     UIImage *theImage=[UIImage imageWithData:data]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     cell.albumImage.image=theImage; 
    }); 
}); 
} 

問題是UICollection視圖滾動緩慢的工作時,我滾動,所以我需要做的滾動faster.I應用一些解決方案,但問題仍然沒有解決。 請幫我解決這個問題。

+1

爲什麼不把圖像放在'xcassets'而不是使用'Documents文件夾'?它提高了加載速度和滾動 –

+0

@LucaD你可以建議我一些小例子,我如何使用Xcassets而不是文檔目錄? –

回答

0

我有同樣的問題。當我在應用程序中進行配置時(滾動時),我可以看到時間在AppleJPEG的某處(不記得確切的名字)。我的圖片太大,不得不縮小(對於單元格的大小),而這恰好在主線程中。

嘗試在後臺線程上呈現圖像。 退房:https://developer.apple.com/videos/play/wwdc2012-211/

0
[cell.albumImage setImage:[UIImage imageWithContentsOfFile:fullPath]]; // set image on main thread 

&

cell.albumImage.image=theImage; 

兩個相同

刪除第一行代碼

[cell.albumImage setImage:[UIImage的 imageWithContentsOfFile:FULLPATH]];

+0

它似乎第一個像一個佔位符 –

0
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", docsDir,[filePathsArray objectAtIndex:indexPath.item]]; 
[cell.albumImage setImage:[UIImage imageWithContentsOfFile:fullPath]]; 
0

您已經使用[cell.albumImage setImage:[UIImage imageWithContentsOfFile:fullPath]];

他們爲什麼要再次使用下面的代碼設置圖像已經設置的圖像。您需要刪除該代碼。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSData *data= [NSData dataWithContentsOfFile:fullPath]; 
     UIImage *theImage=[UIImage imageWithData:data]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     cell.albumImage.image=theImage; 
    }); 
}); 
+0

它仍然工作緩慢的任何一個上述陳述 –

0

這是一項艱鉅的任務做好每滾動

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *inputPath = [[self.arrTbl_Album_Image objectAtIndex:indexPath.row]objectAtIndex:2 ]; 

NSString *imageEx=[inputPath pathExtension]; 
NSString *imageName=[[inputPath lastPathComponent]stringByDeletingPathExtension]; 

NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",imageName,imageEx]]; 

你能避免呢?比方說,使用xcassets?當然,它會更快。檢索圖像的後臺操作根本不是問題,它在背景

ADDED:在這裏你可以看到如何使用xcassetshttps://developer.apple.com/library/ios/documentation/Xcode/Reference/xcode_ref-Asset_Catalog_Format/index.html#//apple_ref/doc/uid/TP40015170-CH18-SW1

+0

你可以建議我一些小例子,我如何使用Xcassets而不是文檔目錄? –

+0

如果我從應用程序文件夾中的照片應用程序添加圖像,然後如果我從照片應用程序中刪除這些圖像,那麼有可能使用xcassets找回這些圖像? –

+0

只需進入xcassets,右鍵單擊圖像 - >在Finder中顯示 –