iphone - 如何在 iPhone 中加载高质量的 .gif 图像

标签 iphone objective-c ios uiimageview animated-gif

通过提取 .gif 图像,我得到了 48 帧,分辨率为 (1024*768)。然后我将这些帧存储在这样的数组中。

NSMutableArray *splashImages = [[NSMutableArray alloc] init];
    for(int i=1;i <= 48;i ++)
    {
        [splashImages addObject:[UIImage imageNamed:[NSString  stringWithFormat:@"img%d.jpg",i]]];
    }
    [ImageView setAnimationImages:splashImages];
    [ImageView setAnimationDuration:1.0];
    [ImageView startAnimating];

我把这个给了 UIImageView,持续时间为 1 秒,通过运行应用程序,它像动画一样流畅,甚至卡住了。

有什么解决方案吗?

最佳答案

首先,如果将48张图片都加载到内存中,恐怕会出现内存警告,请使用Instrument进行检查。

================================

你可能需要强制它在解压缩和渲染之前进行,检查这个 link1还有这个link2

试试这个,它会导致图像解压缩,即使图像上下文只有 1 个像素大。:

UIGraphicsBeginImageContext(CGSizeMake(1, 1));
    [decompressedImage drawAtPoint:CGPointZero];
UIGraphicsEndImageContext();

或者类似这样的东西:

CGDataProviderRef imageDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)imageData);
    CGImageRef image = CGImageCreateWithJPEGDataProvider(imageDataProvider, NULL, NO, kCGRenderingIntentDefault);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef bitmapContext = CGBitmapContextCreate(NULL, CGImageGetWidth(image), CGImageGetHeight(image), CGImageGetBitsPerComponent(image), 0, colorSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
    CGColorSpaceRelease(colorSpace);
    CGContextDrawImage(bitmapContext, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);

    CGDataProviderRelease(imageDataProvider);
    CGContextRelease(bitmapContext);

    if (image){
        decompressedImage = [UIImage imageWithCGImage:image];
        CGImageRelease(image);
    }

希望对您有所帮助。

关于iphone - 如何在 iPhone 中加载高质量的 .gif 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12399839/

相关文章:

ios - 将 ViewController 推到 TabBarController 之上

iphone - UITapGestureRecognizer 在 UIImageView 中不工作

iPhone SDK : NSMutableArray count causes EXC_BAD_ACCESS

iOS swift 'Could not find member executeFetchRequest'

ios - 为什么当 dequeueReusableCellWithIdentifier 发生时不会调用单元格中的 init 方法

iOS推送通知系统服务器

ios - 如何创建 PhoneGap 插件 iOS

iphone - 如何显示登录和关闭设置 - Storyboard

iphone - 如何优化我的 Controller 以使其加载速度更快?

iphone - 在 Objective-C 中创建 POST 请求