2016-03-28 40 views
2

我有多個圖像和視頻存儲在應用程序文檔目錄中。我以這種方式使用SDWebImage在集合視圖中顯示該圖像和視頻。無法更快地將本地存儲的圖像加載到集合視圖中。

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


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

NSInteger Type=[[[self.arrTbl_Album_Image objectAtIndex:indexPath.row] objectAtIndex:3] intValue]; 
if (Type==1) { 
     cell.imgPlay.hidden=YES; 
     [cell.albumImage setImage:[images objectAtIndex:indexPath.row]]; 
    }else{ 
     cell.imgPlay.hidden=NO; 
     [cell.albumImage setImage:[images objectAtIndex:indexPath.row]]; 
    } 

    return cell; 
} 

這裏是生成縮略圖的代碼。

-(void)load_Album_Data{ 
     NSUserDefaults *album_id=[NSUserDefaults standardUserDefaults]; 
     NSInteger Album_id=[album_id integerForKey:@"album_Id"]; 

//From the query. 
NSString *query=[NSString stringWithFormat:@"select * from tbl_Album_Image where album_id='%ld' order by id desc",Album_id]; 


//Get result. 
if (self.arrTbl_Album_Image != nil) { 
    self.arrTbl_Album_Image=nil; 
} 
self.arrTbl_Album_Image=[[NSArray alloc]initWithArray:[self.dbManager loadDataFromDB:query]]; 


dispatch_queue_t myQueue = dispatch_queue_create("MyQueue",NULL); 
dispatch_async(myQueue, ^{ 

for(int i=0;i<self.arrTbl_Album_Image.count;i++) 
{ 
    NSInteger AssetType=[[[self.arrTbl_Album_Image objectAtIndex:i] objectAtIndex:3] intValue]; 
    if (AssetType==1) { 
     //for image 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

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

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

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

    UIImage *img=[UIImage imageWithContentsOfFile:fullPath]; 
    CGFloat scale = MAX(106/img.size.width, 106/img.size.height); 
    CGFloat width = img.size.width * scale; 
    CGFloat height = img.size.height * scale; 
    CGRect imageRect = CGRectMake((106 - width)/2.0f, 
            (106 - height)/2.0f, 
            width, 
            height); 
    CGSize c=CGSizeMake(106, 106); 
    UIGraphicsBeginImageContextWithOptions(c, NO, 0); 
    [img drawInRect:imageRect]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 


    [images addObject:newImage]; 
    } 
    else{ 
     //for video 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

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

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

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

     NSURL *videoURL = [NSURL fileURLWithPath:fullPath]; 
     AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil]; 

     AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; 
     imageGenerator.appliesPreferredTrackTransform = YES; 
     NSError *err = NULL; 
     CMTime time = CMTimeMake(1, 2); 
     CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:&err]; 
     UIImage *thumbNailImage = [[UIImage alloc] initWithCGImage:imageRef]; 


     CGFloat scale = MAX(106/thumbNailImage.size.width, 106/thumbNailImage.size.height); 
     CGFloat width = thumbNailImage.size.width * scale; 
     CGFloat height = thumbNailImage.size.height * scale; 
     CGRect imageRect = CGRectMake((106 - width)/2.0f, 
             (106 - height)/2.0f, 
             width, 
             height); 
     CGSize c=CGSizeMake(106, 106); 
     UIGraphicsBeginImageContextWithOptions(c, NO, 0); 
     [thumbNailImage drawInRect:imageRect]; 
     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 

      [images addObject:newImage]; 

    } 

    dispatch_async(dispatch_get_main_queue(), ^{ 
      //Reload the Image view. 
    [self.album_ImageView reloadData]; 
        } 


    }); 

} 
}); 


} 

但問題是我無法更快地加載圖像,並且當上下滾動發生得很快時,應用程序崩潰。並且還不生成視頻圖像。 請幫我解決這個問題,併爲我提供任何解決方案。 我想在集合視圖單元格中加載快速圖像。

+0

詳細描述了崩潰,消息和堆棧跟蹤 – Wain

+0

@請參見此詳細信息1)收到的內存警告.2)通信錯誤: {count = 1,contents = \t「XPCErrorDescription」=> {length = 22,contents =「連接中斷」} } 3)ImageIO:CGImageSourceCreateWithData數據參數爲零。我滾動上下集合視圖和應用程序崩潰 –

+0

什麼是圖像,他們有多大,什麼格式,哪一個正試圖加載時崩潰(大概這也發生如果你慢慢地向下滾動並等待每個圖像在去下一個單元之前完成)? – Wain

回答

1

,而不是我們的代碼來設置圖像的變化來此

[cell.albumImage sd_setImageWithURL:imaURL placeholderImage:[UIImage imageNamed:@"placeholder.png"] options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 

     dispatch_async(dispatch_get_main_queue(), ^{ 

      cell.albumImage.image=image; 
     }); 
    }]; 

它將加載速度更快,甚至保存在高速緩存

+0

收到內存警告並且應用程序崩潰。 –

+0

你可以給出錯誤爲什麼應用程序終止? –

+0

看到這個細節1)收到的內存warning.2)通信錯誤: {count = 1,contents =「XPCErrorDescription」=> {length = 22,contents =「連接中斷「}}> 3)ImageIO:CGImageSourceCreateWithData數據參數爲零..這一切都出現在我滾動上下集合視圖和應用程序崩潰時 –

0

看來,當你從照片庫中保存圖像,您應該保存等等如果你需要它的全尺寸,並保存適當的縮放版本的圖像,你可以用它來顯示在應用程序中。如果使用的ALAsset可能是thumbnail

如果你這樣做,圖像加載將會很快並且內存使用量最小。

目前,您只是試圖將過多的圖像數據加載到內存中,而且這會花費很長時間並在應用程序不可避免地耗盡時崩潰。使用設備攝像頭拍攝的圖像實際上非常大...

+0

是的,我運用這種方法,並在細胞顯示生成縮略圖。而且還努力爲我好,但仍然有負載的某個時候,當圖像縮略圖生成。看我在編輯我的代碼。告訴我這是否正確。因爲每當我進入特定相冊時圖像加載。 –

+0

如果保存各主圖像到磁盤我保存縮略圖也是如此,在主圖像旁邊有一個目錄,並用相同的名字,這樣你就不需要做這一切的時候。 – Wain