2013-05-24 5 views
2

UICollectionView에 약간의 흐름 문제가 있습니다. Apple iBook 앱처럼 PDF의 축소판을 표시하고 싶습니다. 컬렉션보기를 스크롤 할 때 정말 부드럽지 않은 것을 볼 수 있습니다.컬렉션보기에서 이미지를 빠르게로드하는 방법

+ (UIImage *)generateThumbnailForFile:(NSString *) fileName 
{ 
    // ----- Check if thumbnail already exist 
    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 

    NSString* thumbnailAddress = [documentsPath stringByAppendingPathComponent:[[fileName stringByDeletingPathExtension] stringByAppendingString:@".png"]]; 
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:thumbnailAddress]; 

    if (fileExists) 
     return [UIImage imageWithContentsOfFile:thumbnailAddress]; 

    // ----- Generate Thumbnail 
    NSString* filePath = [documentsPath stringByAppendingPathComponent:fileName]; 

    CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:filePath]; 
    CGPDFDocumentRef documentRef = CGPDFDocumentCreateWithURL(url); 
    CGPDFPageRef pageRef = CGPDFDocumentGetPage(documentRef, 1); 
    CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox); 

    UIGraphicsBeginImageContext(pageRect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextTranslateCTM(context, CGRectGetMinX(pageRect),CGRectGetMaxY(pageRect)); 
    CGContextScaleCTM(context, 1, -1); 
    CGContextTranslateCTM(context, -(pageRect.origin.x), -(pageRect.origin.y)); 
    CGContextDrawPDFPage(context, pageRef); 

    // ----- Save Image 
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); 
    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(finalImage)]; 
    [imageData writeToFile:thumbnailAddress atomically:YES]; 
    UIGraphicsEndImageContext(); 

    return finalImage;  
} 

당신이 어떤 제안이 있습니까 : 썸네일을 얻을

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath: (NSIndexPath *)indexPath 
{ 
    GridCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"gridCell" forIndexPath:indexPath]; 

    ... 

    // Set Thumbnail 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     if ([Tools checkIfLocalFileExist:cell.pdfDoc]) 
     { 
      UIImage *thumbnail = [Tools generateThumbnailForFile:((PDFDocument *)[self.pdfList objectAtIndex:indexPath.row]).title]; 

      dispatch_async(dispatch_get_main_queue(), ^{ 
       [cell.imageView setImage:thumbnail]; 
      }); 
     } 
    }); 

    ... 

    return cell; 
} 

방법 : 여기에 내 사진을로드하는 데 사용하는 방법은?

+0

미리보기 이미지를 캐싱 (저장) 했습니까? –

+0

예 이미했습니다. –

답변

2

https://github.com/rs/SDWebImage을 살펴보십시오. 라이브러리는 비동기 이미지로드 특히 위의 방법에 유용합니다. setImageWithURL:placeholderImage:

로드하는 이미지 또는 빈 png로 해당 메소드를 호출하고 검색하려는 이미지가로드되면 채울 것입니다 보유자. 이렇게하면 앱 속도가 상당히 빨라집니다.

+0

라이브러리를 이용해 주셔서 감사합니다. –

관련 문제