2015-10-24 42 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

相關問題