2013-05-07 17 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