为 UIImageView 动画加载和缓存视网膜图像的问题 [英] Issue with loading and caching retina images for UIImageView animation

查看:70
本文介绍了为 UIImageView 动画加载和缓存视网膜图像的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么

对于我的游戏,我使用 view.animationImages = imagesArray; 创建了几个动画.[查看开始动画];

For my game I'm creating several animations using view.animationImages = imagesArray; [view 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;
}

问题和事实

  • 在没有缓存图像的情况下,由于动画不流畅,我得到了视网膜版本的图像
  • 如果我以这种方式缓存图像,动画就可以了,但使用的是非视网膜版本的图像

请问还有其他方法可以缓存和获取视网膜版本吗?

Please is there some other way to cache and get retina versions?

推荐答案

这些图像是否位于您的应用程序包中?如果是这样,这个逻辑就完全没有必要了,因为 imageWithContentsOfFile: 方法已经在视网膜设备上检测并加载了@2x 图像.

Are these images located in your application bundle? If so this logic is all unnecessary because the imageWithContentsOfFile: method will already detect and load the @2x image on a retina device.

此外,您最好使用 [UIImage imageNamed:] 方法,因为这会自动缓存图像以供重复使用,并立即解压缩,避免手动缓存图像,或使用将其绘制到屏幕外的 CGContext 中.

Also, you'd be better off using the [UIImage imageNamed:] method instead because this automatically caches the image for re-use, and decompresses it immediately, avoiding the need for manually caching the images, or for that trickery with drawing it into an offscreen CGContext.

这篇关于为 UIImageView 动画加载和缓存视网膜图像的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆