ios - 使用 SDWebImage 加载大 gif 导致 iOS 应用程序因内存错误而崩溃

标签 ios crash gif sdwebimage

我正在使用 SDWebImage 框架在 UIScrollView 上的 UIImageView 中从互联网返回的 url 中获取全屏图像,如下所示:

[imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeholder"] options:SDWebImageCacheMemoryOnly completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    if (!error) {
        //added as subview
    } else {
        //show error
    }
}];

当图像格式为jpgpng时,它显示得很好,甚至是一些正常大小的gif。但是当有一个大的 gif 时,就会导致崩溃,XCode 会给出这样的错误:

Message from debugger: Terminated due to Memory Error

更新:每次导致崩溃的gif图像是299px*299px570KB 大。对于那些有兴趣查看 gif 的人,链接是:

http://ww4.sinaimg.cn/large/604e48d0jw1evcn03adjjg208b08bdv0.gif

每次单击预览缩略图时,我都可以重新创建它。 我该如何解决这个问题?使用什么工具来观察根本原因?我做了一些研究,我想这是否是由于我使用了以下选项引起的:SDWebImageCacheMemoryOnly,并且大 gif 占用了我应用程序的大部分内存,因此崩溃了。谢谢。

最佳答案

SDWebImage 有自己的 GIF 支持实现。问题是他们迭代所有帧并预先创建位图:

for (size_t i = 0; i < count; i++) {
    CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
    duration += [self sd_frameDurationAtIndex:i source:source];
    [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
    CGImageRelease(image);
}

另一方面,FLAnimatedImage 按需创建位图并智能地缓存它们。

看看FLAnimatedImage 。以及使用它进行 GIF 播放的图像加载器,例如 Nuke (我的),PINRemoteImage 、FLAnimatedImage_AFNetworking等。或者直接使用FLAnimatedImage。

关于ios - 使用 SDWebImage 加载大 gif 导致 iOS 应用程序因内存错误而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32173846/

相关文章:

ios - UITextView 链接不可点击 iOS 8

ios - 如何遍历可编辑的 UITextView 字符串以检查 Swift 中的某些单词

android - React Native应用程序在Android上启动时崩溃:找不到类 “com.facebook.react.devsupport.DevSupportManagerImpl”

iOS 应用程序在 calendar.dateComponents 上崩溃

html - 如何使用 HTML/CSS 调整动画 GIF 的大小?

ios - HealthKit后台交付

ios - __block 变量改变值

iphone - MFMailComposeViewController 崩溃

android - GIF 在我的 React Native 应用程序的 Android 版本中没有动画

jquery - GIF 在页面加载时没有动画