zoukankan      html  css  js  c++  java
  • IOS相册滑动浏览缓存处理

     

    转载自      http://blog.csdn.net/cubepeng/article/details/7901229

    做过好几个app中都要实现图片的相册浏览的功能,有开源的库可以用,但是我都觉得比较重,自己写另一个比较轻的。其实相册的功能思路是很清楚的,实现滑动中的内存重用和内存的cache。好现在我逐步讲解,如何来构建简单的相册。

     1.首先我们来创建一个UIScrollView。

    1. _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, 320, 480)];  
    2. _scrollView.delegate=self;  
    3. _scrollView.scrollEnabled=YES;  
    4. _scrollView.decelerationRate = UIScrollViewDecelerationRateNormal;  
    5. _scrollView.pagingEnabled=YES;  
    6. _scrollView.showsVerticalScrollIndicator=NO;  
    7. _scrollView.showsHorizontalScrollIndicator=NO;  
    8. _scrollView.backgroundColor = self.view.backgroundColor;  
    9. [_scrollView setContentSize:CGSizeMake(320*[_itemSet count], 316)];  

    (大家注意到有一个_itemSet,这是一个包含了图片数据的集合,可以是网络上的图片的地址,也可以是包含了封装图片地址的对象的实例,总值不能是UIImage的实例,不然就是失去了封装的意义了。)
    1. [self.view addSubview:_scrollView];  
    2. [self configScrowViewWithIndex:self.itemIndex withForward:NO withOrigin:YES];  
    3.  pageIndex = itemIndex;  

    将scrollview添加为当前试图的子试图之后,我们马上 调用

      [selfconfigScrowViewWithIndex:self.itemIndexwithForward:NOwithOrigin:YES];

         这是实现整个业务逻辑的关键方法,就是说去配置当前的索引应该执行的操作,第一个参数是当前的索引,比如说0就代表我是从_itemSet的第一个元素 映射的图片开始显示;第二个参数是判断当前是向前还是向后翻动相册;第三个参数是表示当前是不是第一次显示相册。

        将itemSet的索引赋给页数,这两个成员一直是相等的,这样只是为了区别的看待这两个序数。


    2.实现我们的关键方法:
    - (void)configScrowViewWithIndex:(int)index withForward:(BOOL)isForward withOrigin:(BOOL)isOrigin

    1. - (void)configScrowViewWithIndex:(int)index withForward:(BOOL)isForward withOrigin:(BOOL)isOrigin  
    2. {  
    3.     if ([_itemSet count]<1) {  
    4.         return;   
    5.     }  
    6.     //当偏移量是0的话加载当前的索引的视图和前后的视图(如果存在的话)  
    7.     if (isOrigin) {  
    8.         WPCellImageView *currentView = [self configItemWithIndex:index];  
    9.         if (currentView) {  
    10.             currentView.tag = CURRENTVIEWTAG;  
    11.             [_scrollView addSubview:currentView];  
    12.               
    13.         }  
    14.           
    15.         WPCellImageView *nextView = [self configItemWithIndex:index+1];  
    16.         if (nextView) {  
    17.             nextView.tag = NEXTVIEWTAG;  
    18.             [_scrollView addSubview:nextView];  
    19.               
    20.         }  
    21.         WPCellImageView *lastView = [self configItemWithIndex:index-1];  
    22.         if(lastView)  
    23.         {  
    24.             lastView.tag = LASTVIEWTAG;  
    25.             [_scrollView addSubview:lastView];  
    26.         }  
    27.           
    28.     }  
    29.     else {  
    30.         //如果向前滑动的话,加载下一张试图的后一张试图,同时移除上一张试图的前一张试图  
    31.         if (isForward) {  
    32.             if ([_scrollView viewWithTag:LASTVIEWTAG])  
    33.             {  
    34.   
    35.                 WPCellImageView *view = (WPCellImageView*)[_scrollView viewWithTag:  
    36.                                                                    LASTVIEWTAG];  
    37.                 [view cancelImageLoad];  
    38.                 [[_scrollView viewWithTag:LASTVIEWTAG]removeFromSuperview];//移出前一个视图  
    39.             }  
    40.             if ([_scrollView viewWithTag:NEXTVIEWTAG])   
    41.             {                    
    42.                 //如果下个视图存在  
    43.                 UIView *currentView = [_scrollView viewWithTag:CURRENTVIEWTAG];  
    44.                 currentView.tag = LASTVIEWTAG;  
    45.                 UIView *view =  [_scrollView viewWithTag:NEXTVIEWTAG];  
    46.                 view.tag     = CURRENTVIEWTAG;  
    47.                 WPCellImageView *nextView = [self configItemWithIndex:index+1];  
    48.                 if (nextView) {  
    49.                     nextView.tag = NEXTVIEWTAG;  
    50.                     [_scrollView addSubview:nextView];  
    51.                 }  
    52.                   
    53.             }  
    54.         }  
    55.         //如果向后滑动的话,加载上一张试图的前一张试图,同时移除下一张试图的后一张试图  
    56.         else {  
    57.             if ([_scrollView viewWithTag:NEXTVIEWTAG]) {  
    58.                 [[_scrollView viewWithTag:NEXTVIEWTAG]removeFromSuperview];//移出后一个视图  
    59.                 WPCellImageView *view = (WPCellImageView*)[_scrollView viewWithTag:  
    60.                                                                    NEXTVIEWTAG];  
    61.                 [view cancelImageLoad];  
    62.             }  
    63.             if ([_scrollView viewWithTag:LASTVIEWTAG]) { //如果上个视图存在  
    64.                 UIView *currentView = [_scrollView viewWithTag:CURRENTVIEWTAG];  
    65.                 currentView.tag = NEXTVIEWTAG;  
    66.                 UIView *view =  [_scrollView viewWithTag:LASTVIEWTAG];  
    67.                 view.tag     = CURRENTVIEWTAG;  
    68.                 WPCellImageView *lastView = [self configItemWithIndex:index-1];  
    69.                 if (lastView) {  
    70.                     lastView.tag = LASTVIEWTAG;  
    71.                     [_scrollView addSubview:lastView];  
    72.                 }  
    73.             }  
    74.               
    75.         }  
    76.           
    77.     }  
    78.       
    79. }  


    这里面还调用了一个方法:

    - (WPCellImageView*)configItemWithIndex:(int)index

    这个方法是index所代表的索引的试图,WPCellImageVIew是我定制的一个试图,能够根据图片URL下载图片,有一些开源库可以用,这里就不细说了。下面贴上这个方法的实现。

    1. if (index<0 || index>[_itemSet count]-1) {  
    2.       DLog(@"%d",[_itemSet count]);  
    3.       return nil;  
    4.   }  
    5.   WPWaterflowItem *dic      = [_itemSet objectAtIndex:index];  
    6.     
    7.   WPCellImageView *firstView = [[WPCellImageView alloc]initWithFrame:CGRectMake(_scrollView.frame.size.width*index, 0, _scrollView.frame.size.width, _scrollView.frame.size.height) ];  
    8.   [firstView setURL:dic.picURL];  
    9.   return [firstView autorelease];  

    3.通过UIScrollview的代理方法实现图片的显示和内存管理

    1. #pragma mark UIScrollView Delegate   
    2. - (void)scrollViewDidScroll:(UIScrollView *)scrollView  
    3. {  
    4.     CGFloat pageWidth = scrollView.frame.size.width;  
    5.     int beforeIndex = pageIndex;  
    6.     pageIndex = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;  
    7.     if (pageIndex>beforeIndex) {  
    8.         itemIndex ++;  
    9.         [self configScrowViewWithIndex:itemIndex withForward:YES withOrigin:NO];  
    10.     }  
    11.     else if(pageIndex<beforeIndex) {  
    12.         itemIndex --;  
    13.         [self configScrowViewWithIndex:itemIndex withForward:NO withOrigin:NO];  
    14.           
    15.     }  
    16.       
    17. }  

    呵呵,很简单吧,滑动超过半页的时候就去加载图片和管理内存。


    总结:总体的思路就是在一个scroolview一直保存 3张图片,保证比较好的内存管理和流畅。

  • 相关阅读:
    Leetcode: Longest Absolute File Path
    Leetcode: Mini Parser
    Leetcode: First Unique Character in a String
    Leetcode: Lexicographical Numbers
    Leetcode: Shuffle an Array
    Leetcode: Ransom Note
    Leetcode: Linked List Random Node
    Socket网络编程--聊天程序(7)
    Socket网络编程--聊天程序(6)
    Socket网络编程--聊天程序(5)
  • 原文地址:https://www.cnblogs.com/allanliu/p/4203509.html
Copyright © 2011-2022 走看看