gpt4 book ai didi

ios - CGImageSourceCreateWithData 继续生成错误

转载 作者:行者123 更新时间:2023-11-29 01:32:19 25 4
gpt4 key购买 nike

我的代码遇到了问题。我似乎无法创建 CGImageSource/CGImageSourceRef,但是我看到的每个 repo 都使用完全相同的方法。我已经测试过数据对象是否包含 gif,确实如此。所以我已将此错误隔离到 CGImageSourceCreateWithData 函数,但我不知道如何修复它。任何帮助将不胜感激。

这是错误:

warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.

这个函数总是失败:

var imgSource = CGImageSourceCreateWithData(data, nil)

这是我不断失败的简单代码:

import UIKit
import ImageIO
import MobileCoreServices
import AVFoundation

class LCGIFImage: UIImage {

//MARK: Initializers

convenience override init?(contentsOfFile path: String) {
let data = NSData(contentsOfURL: NSURL(string: path)!)
self.init(data: data!)
}

convenience override init?(data: NSData) {
self.init(data: data, scale: 1.0)
}

override init?(data: NSData, scale: CGFloat) {
var imgSource = CGImageSourceCreateWithData(data, nil)


super.init(data: data, scale: scale)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

required convenience init(imageLiteral name: String) {
fatalError("init(imageLiteral:) has not been implemented")
}
}



class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
let path = NSBundle.mainBundle().URLForResource("1", withExtension: "gif")?.absoluteString as String!
let test = LCGIFImage(contentsOfFile: path)

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

最佳答案

您是否看过崩溃的堆栈跟踪?我试过你的代码,问题似乎是初始化程序被递归调用。

enter image description here

你调用 super.init(data: data, scale: scale),然后它调用 self.init(data: data),它调用你方便的初始化器,然后调用你指定的初始化器,这个初始化器再次调用 super.init(data: data, scale: scale)

老实说,我不会子类化 UIImage,在幕后涉及到 CGImageRef 的很多桥接(很可能涉及魔术)。如果您坚持子类化 UIImage,请调用指定的初始化程序 super.init(data: data) 而不是 super.init(data: data, scale: scale)

关于ios - CGImageSourceCreateWithData 继续生成错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33324582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com