Reputation: 2214
I'm creating gif splitter that returns image sequence from given gif file, it works well with small size gifs and everything look as desired but with big files (bigger than 500×500 px) causing memory issue.
Xcode message : Message from debugger: Terminated due to memory issue “Connection interrupted” Communications error
Bigger than this problem I can't see any leak or load in XCode analyzer, memory usage shows between 50 - 110 mb ?!
I tried to change gifOptions
but nothing changed.
import UIKit
import ImageIO
import MobileCoreServices
class CGifManager {
static let shared = CGifManager()
public func getSequence(gifNamed: String) -> [UIImage]? {
guard let bundleURL = Bundle.main .url(forResource: gifNamed, withExtension: "gif") else {
print("This image named \"\(gifNamed)\" does not exist!"); return nil
}
guard let imageData = try? Data(contentsOf: bundleURL) else {
print("Cannot turn image named \"\(gifNamed)\" into NSData") return nil
}
let gifOptions = [ kCGImageSourceShouldAllowFloat as String : true as NSNumber,
kCGImageSourceCreateThumbnailWithTransform as String : true as NSNumber,
kCGImageSourceCreateThumbnailFromImageAlways as String : true as NSNumber
] as CFDictionary
guard let imageSource = CGImageSourceCreateWithData(imageData as CFData, gifOptions) else {
debugPrint("Cannot create image source with data!"); return nil
}
let framesCount = CGImageSourceGetCount(imageSource)
var frameList = [UIImage]()
for index in 0 ..< framesCount {
if let cgImageRef = CGImageSourceCreateImageAtIndex(imageSource, index, nil) {
let uiImageRef = UIImage(cgImage: cgImageRef)
frameList.append(uiImageRef)
}
}
return frameList
}
}
This code tested with gif sized 400 x 400 and 400 x 500 (30 frames) and worked as well but actually I need to be work with all sizes, so I no have idea where is the problem?
Any helps acceptable.
Upvotes: 0
Views: 331
Reputation: 535316
This is the source of one of your main problems:
var frameList = [UIImage]()
Alarm bells! Holding an array of UIImages in memory is a great way to run out of memory. Keep images saved to disk and make an array of their names or URLs.
Upvotes: 2