我正在制作一个应用程序,它在UIImageView中显示gif,并从parse中加载gif。我偶然发现的问题是每当我加载一个Gif时,它会使用大约20 MB的内存,并且当我执行一个segue时,这个内存不会被分配。我从UIImageView中删除图像本身,但它仍然不释放内存。显示gif时出现巨大的内存使用Swift iOS

这是我用来显示GIF代码:

extension UIImage {

public class func gifWithData(data: NSData) -> UIImage? {

guard let source = CGImageSourceCreateWithData(data, nil) else {

print("SwiftGif: Source for the image does not exist")

return nil

}

return UIImage.animatedImageWithSource(source)

}

public class func gifWithName(name: String) -> UIImage? {

guard let bundleURL = NSBundle.mainBundle().URLForResource(name, withExtension: "gif") else {

print("SwiftGif: This image named \"\(name)\" does not exist")

return nil

}

guard let imageData = NSData(contentsOfURL: bundleURL) else {

print("SwiftGif: Cannot turn image named \"\(name)\" into NSData")

return nil

}

return gifWithData(imageData)

}

class func delayForImageAtIndex(index: Int, source: CGImageSource!) -> Double {

var delay = 0.1

// Get dictionaries

let cfProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil)

let gifProperties: CFDictionaryRef = unsafeBitCast(

CFDictionaryGetValue(cfProperties,

unsafeAddressOf(kCGImagePropertyGIFDictionary)),

CFDictionary.self)

// Get delay time

var delayObject: AnyObject = unsafeBitCast(

CFDictionaryGetValue(gifProperties,

unsafeAddressOf(kCGImagePropertyGIFUnclampedDelayTime)),

AnyObject.self)

if delayObject.doubleValue == 0 {

delayObject = unsafeBitCast(CFDictionaryGetValue(gifProperties,

unsafeAddressOf(kCGImagePropertyGIFDelayTime)), AnyObject.self)

}

delay = delayObject as! Double

if delay < 0.1 {

delay = 0.1 // Make sure they're not too fast

}

return delay

}

class func gcdForPair(var a: Int?, var _ b: Int?) -> Int {

// Check if one of them is nil

if b == nil || a == nil {

if b != nil {

return b!

} else if a != nil {

return a!

} else {

return 0

}

}

// Swap for modulo

if a < b {

let c = a

a = b

b = c

}

// Get greatest common divisor

var rest: Int

while true {

rest = a! % b!

if rest == 0 {

return b! // Found it

} else {

a = b

b = rest

}

}

}

class func gcdForArray(array: Array) -> Int {

if array.isEmpty {

return 1

}

var gcd = array[0]

for val in array {

gcd = UIImage.gcdForPair(val, gcd)

}

return gcd

}

class func animatedImageWithSource(source: CGImageSource) -> UIImage? {

let count = CGImageSourceGetCount(source)

var images = [CGImageRef]()

var delays = [Int]()

// Fill arrays

for i in 0..

// Add image

if let image = CGImageSourceCreateImageAtIndex(source, i, nil) {

images.append(image)

}

// At it's delay in cs

let delaySeconds = UIImage.delayForImageAtIndex(Int(i),

source: source)

delays.append(Int(delaySeconds * 1000.0)) // Seconds to ms

}

// Calculate full duration

let duration: Int = {

var sum = 0

for val: Int in delays {

sum += val

}

return sum

}()

// Get frames

let gcd = gcdForArray(delays)

var frames = [UIImage]()

var frame: UIImage

var frameCount: Int

for i in 0..

frame = UIImage(CGImage: images[Int(i)])

frameCount = Int(delays[Int(i)]/gcd)

for _ in 0..

frames.append(frame)

}

}

// Heyhey

let animation = UIImage.animatedImageWithImages(frames,

duration: Double(duration)/1000.0)

return animation

}

class func FinalFrame(source: CGImageSource) -> UIImage? {

let count = CGImageSourceGetCount(source)

var images = [CGImageRef]()

var delays = [Int]()

var finalFrame = UIImage()

// Fill arrays

for i in 0..

// Add image

if let image = CGImageSourceCreateImageAtIndex(source, i, nil) {

images.append(image)

}

// At it's delay in cs

let delaySeconds = UIImage.delayForImageAtIndex(Int(i),

source: source)

delays.append(Int(delaySeconds * 1000.0)) // Seconds to ms

}

// Get frames

let gcd = gcdForArray(delays)

var frames = [UIImage]()

var frame: UIImage

var frameCount: Int

for i in 0..

frame = UIImage(CGImage: images[Int(i)])

frameCount = Int(delays[Int(i)]/gcd)

for _ in 0..

frames.append(frame)

}

}

finalFrame = frames[frames.count-1]

return finalFrame

}

}

难道这是原因,我得到的内存问题? 感谢任何帮助!

+0

两者GIF和APNG可以以低存储器请求数,像这样进行处理:http://stackoverflow.com/a/25478854/763355 –

ios html gif 显示,显示gif时出现巨大的内存使用Swift iOS相关推荐

  1. 王者ios系统怎么查自己在哪个服务器,安卓王者荣耀怎么查看iOS系统的 | 手游网游页游攻略大全...

    发布时间:2016-06-29 王者荣耀PVE六国远征系统玩法,冒险模式是非常有意思的玩法,需要合理搭配参战英雄和适当的策略,小编为大家带来了王者荣耀PVE六国远征详解. --知己知彼了解六国远征规则 ...

  2. html5图片上传时IOS和Android均显示摄像头拍照和图片选择

     最近在做信开发时,发现<input type="file" />在IOS中可以拍照或从照片图库选择,而Android系统则显示资源管理器,无拍照选项,网上查找资料 ...

  3. 【Fiddler】iOS机抓取https数据时,失败,全部显示“Tunnel to......443”和手机代理后无法上网的现象

    前置描述: 根据以往的抓包经验设置了ios后,(如 fiddler设置https,ios安装证书设置信任证书等),前面两天还能正常抓取的,但是再今天使用时突然就无法抓包了,且做了代理之后也无法上网了. ...

  4. flutter_webview Ios端请求网页显示空白

    flutter_webview Ios端请求网页显示空白 在使用flutter_webview插件时,请求网页在Android端运行正常,但在IOS端一些网页可以正常打开而一些网页无法打开. 经过排查 ...

  5. 微信小程序IOS机型时间格式显示NaN异常问题

    new Date()转换时间时,IOS机型时间格式显示NaN 错误原因: ios不支持时间为2020-05-29这种格式的日期,必须转换为2020/05/29这种格式才能使用new Date()进行转 ...

  6. SQL Server2008附加数据库之后显示为只读时解决方法

    SQL Server2008附加数据库之后显示为只读时解决方法 啰嗦的话就不多说了,直入主题吧! 方案一: 碰到这中情况一般是使用的sa账户登录的,只要改为Windows身份验证,再附加数据库即可搞定 ...

  7. 动态判断时间插件显示到年月日时分秒

    根据得到的时间格式,动态判断时间插件显示到年月日时分秒 主要split切割字符串来判断 split将一个字符串切割为字符串数组 <!DOCTYPE html> <html>&l ...

  8. arcgis 出图背景_ArcGIS中导出数据时老显示导出失败,显示“保存对象时出错”什么原因?...

    当我们将数据导出出,有时会导出失败,弹出以下对话框,显示"保存对象时出错" 很多人一度怀疑是自己的数据或者软件出现了问题 因为在别人的电脑上同样的步骤操作成功了 那么究竟是什么原因 ...

  9. 解决alert在ios版微信中显示url的问题(重写alert)

    为了解决alert在ios版微信中显示url的问题 window.alert = function(name){var iframe = document.createElement("IF ...

最新文章

  1. 腾讯 AI 2019这一年
  2. [k8s] 第八章 数据存储
  3. 爬虫-scrapy的中间件
  4. AI独角兽增援抗疫拉锯战,旷视守“城”,依图出“机”
  5. 动态半导体ram依据什么存储信息_LPDDR4X和RAM两者有什么关系 它们有什么特点
  6. hdfs web_ui深入讲解、服务启动日志分析、NN SNN关系
  7. 一个类中有一个参数,需要在另一个类中用到
  8. matlab求系统根轨迹和系统增益,控制系统的根轨迹分析
  9. Luogu 2939 [USACO09FEB]改造路Revamping Trails Luogu 4568 [JLOI2011]飞行路线
  10. vue在新的标签页打开pdf文件
  11. 区块链开发(五)搭建以太坊区块链浏览器
  12. perror 函数用法
  13. java类 家族成员 姓氏_java题目 将一些学生分别按姓氏分类,每个姓氏的学生输出到一行上。...
  14. 非线性方程(组)的求解
  15. 数字IC笔记-scan chain 压缩和解压缩
  16. VS2019 内存占用高
  17. 计算机硬盘类型及特点,硬盘有几种类型其特点分别是什么
  18. Dubbo之HTTP RPC vs Dubbo RPC性能压测
  19. Retinaface+Arcface实现视频人脸识别
  20. 天王星-颠倒了的世界

热门文章

  1. 面试干货 | Java 能否自定义一个类叫 java.lang.System?
  2. 【MATLAB】Parzen窗与K近邻算法原理与代码详解
  3. UNDO Tablespace
  4. CentOS 8 安装 Docker  报错  requires containerd.io >= 1.4.1, but none of the providers can be installed
  5. 数据集_自动驾驶数据集
  6. 跨域会报40几_关于跨域,以及跨域的几种方式
  7. 放大缩小保证div对齐_NFS Write IO 不对齐深度分析
  8. python运用在哪些地方_必看 | 2020年,Python十大应用领域介绍!
  9. python找与7相关的数_用python统计并输出1000以内所有能同时被3和7整除的数的个数?...
  10. js计算排名_今天,我们讲一下,快速排名与黑帽SEO