2014-07-07 2 views
0

url.So의 사전 저장소 이미지를 변경할 수있는 배열이 있습니다. 응용 프로그램 캐시에 이미지를 저장하는 방법과 인터넷에 연결되어 있지 않을 때 이미지를 볼 수 있습니다.캐시에 이미지 데이터를 저장하는 방법

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString * [email protected]"CustomCell"; 
    CustomCell *cell = (CustomCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIndentifier forIndexPath:indexPath]; 
    NSDictionary *dictVideo = [self.videoList objectAtIndex:indexPath.row]; 
    [cell.indicator startAnimating]; 
    //set title 
    NSString *titleVideo = [dictVideo objectForKey:@"Title"]; 
    [cell.myLabel setText:titleVideo]; 
    // set image url 
    NSString *urlVideo = [dictVideo objectForKey:@"Url"]; 
    NSURL *url = [NSURL URLWithString:urlVideo]; 
    cell.imageView.image = nil; 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSData *data = [NSData dataWithContentsOfURL:url]; 
     UIImage * img = [[UIImage alloc]initWithData:data]; 
     dispatch_sync(dispatch_get_main_queue(), ^{ 
      NSLog(@"%ld %@ %@",(long)indexPath.row,titleVideo,url); 
       [cell.imageView setImage: img]; 
       [cell.indicator stopAnimating]; 
     }); 
      }); 
      return cell; 
    } 
+0

'캐시'를 정의합니다. 로컬 sqlite, 문서 폴더? 이미 원시 데이터가 있으므로 몇 가지 옵션이 있습니다. 예 :'[[NSFileManager defaultManager] createFileAtPath : 경로 내용 : 데이터 속성 : nil]; ' – SwiftArchitect

+0

인터넷에서 다운로드 한 이미지를 문서의 캐시 폴더에 저장하여 이미지를 더 빨리로드하려고합니다. – HIEU

+0

나는 여기와 같은 질문에 대답합니다. NSCache를 사용하여 이미지 데이터를 캐싱하는 것을 포함하여 ... http://stackoverflow.com/questions/15799432/poor-uicollectionview-scrolling-performance-with-uiimage/15799771#15799771 – danh

답변

0

NSURLCache는 이미이 작업을 수행합니다. 응용 프로그램이 이미 디스크 지속성에 없을 수 있습니다 암시 NSURLCache을 가지고, 그래서 당신은 명시 적으로 설정하려면 :

NSURLCache *cache = [[NSURLCache alloc]initWithMemoryCapacity:(5 * 1024 * 1024) diskCapacity:(10 * 1024 *1024) diskPath:@"NSURLCache.db"]; 
[NSURLCache setSharedURLCache:cache]; 

그리고 그 시점에서 URL 로딩 시스템은 메모리와 디스크에 데이터를 모두 저장할에

. 이렇게하면 연결이 없을 때 사용할 수 있습니다. 평소와 같이 계속 요청을하면 캐시에서 처리 될 때 훨씬 빠르게 돌아갑니다.

이 동작을 수정하려면 NSURLRequest에서 사용하는 캐시 정책 (예 : NSURLRequestReturnCacheDataElseLoad 또는 NSURLRequestReturnCacheDataDontLoad)을 수정해야합니다. 죄송 합니다만, dataWithContentsOfURL:을 사용하고 있으며이를 수행 할 기회가 없습니다. NSURLSessionTask 또는 NSURLConnection을 사용하면 그렇게 할 수 있습니다.

서버는 클라이언트에게 응답의 유효 기간을 알려주고 기본 캐시 정책은이를 준수합니다.

1

우마르 파 루크가 말한대로 당신이 다음

#import "UIImageView+WebCache.h" 
UIImage *Noimg = [UIImage imageNamed:@"noimg.png"]; 
    [cell.PlaceImg setImageWithURL:[NSURL URLWithString:strImgname] placeholderImage:Noimg]; 

여기 Noimg이 자리 표시 자 이미지 당신의하는 .m 파일에 코드를 아래의 자동을 사용할 수 있습니다 6, 7 모두 iOS 용, 그것의 지원을 SDWebImage 라이브러리를 사용할 수 있습니다 이미지 위치에 이미지가없는 경우 라이브러리로 관리

관련 문제