2013-05-07 16 views
0

我有什麼問題與加載和緩存視網膜圖像進行的UIImageView動畫

對於我的遊戲我創建使用view.animationImages = imagesArray幾個動畫; [查看startAnimating];

在我的動畫輔助類​​我用這個

- (UIImage *)loadRetinaImageIfAvailable:(NSString *)path 
{ 
    NSString *retinaPath = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@@2x.%@", [[path lastPathComponent] stringByDeletingPathExtension], [path pathExtension]]]; 

    if ([UIScreen mainScreen].scale == 2.0 && [[NSFileManager defaultManager] fileExistsAtPath:retinaPath] == YES) 
     return [[UIImage alloc] initWithCGImage:[[UIImage imageWithData:[NSData dataWithContentsOfFile:retinaPath]] CGImage] scale:2.0 orientation:UIImageOrientationUp]; 
    else 
     return [UIImage imageWithContentsOfFile:path]; 
} 

- (NSMutableArray *)generateCachedImageArrayWithFilename:(NSString *)filename extension:(NSString *)extension andImageCount:(int)count 
{ 
    _imagesArray = [[NSMutableArray alloc] init]; 
    _fileExtension = extension; 
    _imageName = filename; 
    _imageCount = count; 

    for (int i = 0; i < count; i++) 
    { 
     NSString *tempImageNames = [NSString stringWithFormat:@"%@%i", filename, i]; 
     NSString *imagePath = [[NSBundle mainBundle] pathForResource:tempImageNames ofType:extension]; 

     UIImage *frameImage = [self loadRetinaImageIfAvailable:imagePath]; 
     UIGraphicsBeginImageContext(frameImage.size); 
     CGRect rect = CGRectMake(0, 0, frameImage.size.width, frameImage.size.height); 
     [frameImage drawInRect:rect]; 
     UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 

     [_imagesArray addObject:renderedImage]; 

     if (_isDoublingFrames) 
     { 
      [_imagesArray addObject:renderedImage]; 
     } 
     else if (_isTriplingFrames) 
     { 
      [_imagesArray addObject:renderedImage]; 
      [_imagesArray addObject:renderedImage]; 
     } 

     NSLog(@"filename = %@", filename); 
    } 

    return _imagesArray; 
} 

問題和事實

  • 沒有緩存我得到了圖像的視網膜版本的圖片BYT我的動畫不流利
  • 如果我以這種方式緩存圖像,動畫可以,但使用非視網膜版本的圖像

請問是否有其他方式緩存和獲取視網膜版本?

回答

1

這些圖像是否位於您的應用程序包中?如果是這樣的話,那麼這個邏輯就沒有必要了,因爲imageWithContentsOfFile:方法已經能夠檢測並加載視網膜設備上的@ 2x圖像。

此外,您最好使用[UIImage imageNamed:]方法,因爲它會自動緩存圖像以供重複使用,並立即解壓,避免手動緩存圖像或欺騙並將其繪製到屏幕外的CGContext中。

+0

是的,所有圖像都在應用程序包中。嗯,與imageNamed是有趣的,因爲我在某處閱讀相反:/因此,我用這個瘋狂的CGContext的東西。 – alexhajdu 2013-05-13 14:33:34

+0

我也使用所有圖像http://imageoptim.com與項目壓縮PNG文件設置爲否。 – alexhajdu 2013-05-13 14:35:36

+1

Fwiw,Apple建議禁止在Xcode中禁用壓縮PNG文件。它執行ImageOptim不支持的其他優化,例如字節交換,所以即使圖像會更大,它們也會更快地加載和顯示。 – 2013-05-16 07:42:50