2014-06-25 53 views
0

落後我使用這個:https://github.com/nicklockwood/SwipeViewSwipeview對項目變更

我在我的應用程序2米不同的地方使用它。在這一部分上的屏幕1-3mb文件大小

  • 150×150的圖像在屏幕上大約100KB的文件大小,沒有問題

    1. 30×30的圖像,這些都是通過IOS相機拍攝的照片。

    在第二次調用swipeViewCurrentItemIndexDidChange方法時,應用程序滯後大約0.5秒,這對用戶不會產生良好的體驗。我知道它正在發生,因爲圖像正在加載,我試圖改進過程,但我沒有成功。我希望加快速度,這樣用戶就無法感覺到物品正在裝載。圖像從設備加載,不涉及互聯網連接。

    我知道這些操作都是在圖庫和facebook應用程序中始終進行,沒有滯後。所以我的代碼或者我使用的這個庫都有問題。

    http://pastebin.com/XyCaRaig

  • +0

    也許你應該只是在開始時加載圖像,只是創建一個'UIImages'而不是文件名的數組。 – carloabelli

    +0

    然後它吃太多的內存,我用另一個滑塊。這個滑塊似乎並不像那樣工作 – Esqarrouth

    +1

    我不太確定單獨加載圖像並將它們一次加載到數組中會如何使用顯着不同的內存量。你在第二句話中究竟是什麼意思? – carloabelli

    回答

    1
    if(self.pictures[index] != NULL) //(self.pictures[index] != NULL) //(button == nil) //(self.currentArray[index][2] != NULL) 
        { 
        UIImage *image = [UIImage imageWithContentsOfFile:self.pictures[index]]; 
        button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    
        NSString *machineName = [MasterClass whichMachine]; 
        if ([machineName isEqual:@"ipad"]) { 
         button.frame = CGRectMake(0.0, 0.0, 220, 220); 
        } else if ([machineName isEqual:@"iphone4"]) { 
         button.frame = CGRectMake(0.0, 0.0, 120, 120); 
        } else if ([machineName isEqual:@"ipod"]) { 
         button.frame = CGRectMake(0.0, 0.0, 120, 120); 
        } 
        else{ 
         button.frame = CGRectMake(0.0, 0.0, 140, 140); 
        } 
         [button setBackgroundImage:image forState:UIControlStateNormal]; 
         ..... 
    

    我注意到,您在使用小面積的全分辨率圖像,並計算成本了很多不必要的資源從CPU/GPU的負載。對你而言,幸運的是,它並沒有崩潰。我建議你壓縮顯示在按鈕上的圖像的大小,並在必要時用全分辨率顯示它。 Facebook就是這麼做的。

    0

    我不知道Swipeview但有兩件事我會取決於什麼是可能用它做嘗試。

    1-修改插件預加載或自己加載它們的圖片數量,然後將它們提供給插件。如果您在屏幕上顯示3張圖像,並且用戶可以在不分頁的情況下雙向滾動,則可能需要預載10-20張圖像

    2-帶後臺線程,遍歷用戶滑動範圍內的所有圖像-20圖像範圍應該足夠)加載圖形庫中的圖像,縮小圖像並將其保存在緩存文件夾中。它應該只加載該文件夾中已經優化的圖像以顯示用戶。

    1

    150x150屏幕上的圖像1-3MB的文件大小,這些都是拍攝的照片 ios相機。

    這是我的兩個建議,以加快速度。

    建議1

    ALAssetsLibrary庫將在一個單獨的線程運行。我建議在主線程中做UI相關的東西。在ALAssetsLibrary區塊內使用-performSelectorOnMainThread:dispatch_sync(dispatch_get_main_queue()將解決您的問題。

    實施例:

    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 
         [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *needToStop) { 
    
         dispatch_sync(dispatch_get_main_queue(), ^{ 
    
          UIImage *image = [UIImage imageWithCGImage:result.defaultRepresentation.fullScreenImage]; 
          //do UI stuff here. 
    
        }); }]; } 
    
        failureBlock:^(NSError *error) { 
           NSLog(@"%@",error.description); 
        }]; 
    

    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 
         [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *needToStop) { 
    
          UIImage *image = [UIImage imageWithCGImage:result.defaultRepresentation.fullScreenImage]; 
          [self performSelectorOnMainThread:@selector(usePhotolibraryimage:) withObject:image waitUntilDone:NO]; 
    
         }]; } 
    
        failureBlock:^(NSError *error) { 
           NSLog(@"%@",error.description); 
        }]; 
    
    - (void)usePhotolibraryimage:(UiImage *)myImage{ 
    
        //do UI stuf here. 
    } 
    

    建議2

    使用AlAsset aspectRatioThumbnail代替fullResolutionImage高性能

    dispatch_sync(dispatch_get_main_queue(), ^{ 
    
        CGImageRef iref = [myasset aspectRatioThumbnail]; 
        //CGImageRef iref = [myasset thumbnail]; 
        UIImage *image = [UIImage imageWithCGImage:iref]; 
    
    }); 
    
    +0

    這些看起來像是要工作,但我找不到在我的代碼中實現它們的方法。有什麼建議麼? – Esqarrouth

    +0

    @Esq:請提供swipeViewCurrentItemIndexDidChange方法相關的代碼庫。我會修好它。 –

    +0

    或照片庫圖像獲取代碼庫..或者您可以發送最新的示例應用程序到[email protected] –

    0

    Facebook應用程序使用https://github.com/rs/SDWebImage加載圖片...

    ,首先你有滯後,因爲你不重用SwipeViewDataSource的意見。你需要做這樣的事情

    - (UIView *)swipeView:(__unused SwipeView *)swipeView viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view 
    { 
        UIButton *button = (UIButton *)view; 
        if(!button) { 
         button = [UIButton buttonWithType:UIButtonTypeCustom]; 
        } 
    
        NSString *imageFile = self.pictures[index]; 
        NSLog(@"Image that will be loaded : %@",imageFile); 
    
        if(imageFile) { 
         UIImage *image = [UIImage imageWithContentsOfFile:imageFile]; 
    
         NSString *machineName = [MasterClass whichMachine]; 
         if ([machineName isEqual:@"ipad"]) { 
          button.frame = CGRectMake(0.0, 0.0, 220, 220); 
         } else if ([machineName isEqual:@"iphone4"]) { 
          button.frame = CGRectMake(0.0, 0.0, 120, 120); 
         } else if ([machineName isEqual:@"ipod"]) { 
          button.frame = CGRectMake(0.0, 0.0, 120, 120); 
         } else{ 
          button.frame = CGRectMake(0.0, 0.0, 140, 140); 
         } 
    
         [button setBackgroundImage:image forState:UIControlStateNormal]; 
         button.tag = 100 + index; 
    
         [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
        } 
    
        return button; 
    } 
    

    ,然後,主要的問題是圖像擴展...該系統需要在屏幕上顯示之前的圖像解壓縮和需要在後臺做(如果你不「不想滯後)... 解壓縮圖像是有點棘手,所以請儘量使用SDWebImage,因爲它會在後臺爲你解壓縮的圖像;)

    加載本地文件與SDWebImage看到這一點:https://github.com/rs/SDWebImage/issues/153

    如果你想讀一點關於這個問題,你可以檢查這個可可的舊帖子etics:http://www.cocoanetics.com/2011/10/avoiding-image-decompression-sickness/

    +0

    當我寫!按鈕時,它顯着減少了滯後。但讓我們說在我提供的代碼中按照這個順序命名爲0,1,2,3,4,5,6,7。當我添加!按鈕時,它們的順序變成0,1,2,0,1,2。所以我得不到第三張照片。 – Esqarrouth

    +0

    我已經更改了答案中的代碼,嘗試使用我的方法版本(基於您的代碼)並檢查日誌以查看將要加載的圖像文件是否正確... – Zasuk

    +0

    http://pastebin.com/sLgYs9Au 我向右滑動3次,向左滑動3次。它確實有效,但完全像我做的那樣。它沒有加速,仍然滯後。 – Esqarrouth