2015-10-24 37 views
0

我正面临着我的代码问题。我似乎无法创建一个CGImageSource/CGImageSourceRef,但是我看到的每个回购都使用了完全相同的方法。我已经测试过,看看数据对象是否包含gif,并且它确实如此。所以我已经将此错误隔离为CGImageSourceCreateWithData函数,我不知道如何解决它。任何帮助,将不胜感激。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. 
} 
} 
+0

令路径= NSBundle.mainBundle()pathForResource( “1”,ofType: “GIF”)。 –

+0

@LeoDabus感谢。但是,这个问题不在路径中。它能够找到gif文件。这是'CGImageSourceCreateWithData'函数一直给我的警告:无法加载任何Objective-C类信息。这将显着降低可用类型信息的质量。 – iamktothed

回答

1

有你看了堆栈跟踪你的崩溃?我试过你的代码,问题似乎是递归调用初始化器。

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)

+0

这有效。然而,我感到困惑的是,在Swift 1.2,iOS 8,Xcode 6中有相同的初始化模式。另外,如果我错了,请纠正我,但是UIImage没有指定的初始化程序?如果是这样,[在指定委托人时指定委托人的便利性](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097 -CH18-ID216)。如果是这样,我所遵循的init模式不应该陷入递归模式。 – iamktothed

相关问题