Swift中Call can throw, but it is not marked with 'try' and the error is not handled錯(cuò)誤的解決

最近使用Swift編程中帘睦,遇到一個(gè)問題,就是出現(xiàn)了 Call can throw, but it is not marked with 'try' and the error is not handled 的錯(cuò)誤坦康。
需要獲取視頻的某一幀的圖片竣付,使用copyCGImageAtTime(requestedTime: CMTime, actualTime: UnsafeMutablePointer<CMTime>) throws -> CGImage 但是使用該方法時(shí)出現(xiàn)了錯(cuò)誤:Call can throw, but it is not marked with 'try' and the error is not handled , 剛開始以為是參數(shù)的錯(cuò)誤,因?yàn)樵贠C里面方法是這樣的 - (nullable CGImageRef)copyCGImageAtTime:(CMTime)requestedTime actualTime:(nullable CMTime *)actualTime error:(NSError * __nullable * __nullable)outError

首先先看一下這個(gè)方法的作用是什么滞欠?

//  獲取單幀的圖片
- (nullable CGImageRef)copyCGImageAtTime:(CMTime)requestedTime actualTime:(nullable CMTime *)actualTime error:(NSError * __nullable * __nullable)outError
// 根據(jù)視頻的路徑獲取單幀圖片
- (UIImage *)getThumbImage:(NSURL *)url {
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    gen.appliesPreferredTrackTransform = YES;
    CMTime time = CMTimeMakeWithSeconds(0.0, 1);
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);
    return thumb;
}

在OC中這是一個(gè)很常見的方法古胆,但轉(zhuǎn)到Swift里面,方法發(fā)生了一些變化筛璧,但原理是不變的逸绎,但是卻出現(xiàn)了問題妖滔,

問題截圖:


出問題了

swift里面方法是這樣的

/*! 
@method copyCGImageAtTime:actualTime:error: 
@abstract Returns a CFRetained CGImageRef for an asset at or near the specified time. 
@param requestedTime  The time at which the image of the asset is to be created. 
@param actualTime  A pointer to a CMTime to receive the time at which the image was actually generated. If you are not interested in this information, pass NULL. 
@param outError  An error object describing the reason for failure, in the event that this method returns NULL. 
@result A CGImageRef. 
@discussion Returns the CGImage synchronously. Ownership follows the Create Rule. */ 
 public func copyCGImageAtTime(requestedTime: CMTime, actualTime: UnsafeMutablePointer<CMTime>) throws -> CGImage

這里出現(xiàn)的throws是個(gè)什么鬼啊桶良?? 怎樣解決呢沮翔?陨帆?


其實(shí)很簡(jiǎn)單,原因就是沒有處理錯(cuò)誤 采蚀。我們根據(jù)錯(cuò)誤提示疲牵,調(diào)用可以拋出,但它沒有標(biāo)記和錯(cuò)誤處理通過加一個(gè)try解決榆鼠。
(PS: 就像Java中的異常錯(cuò)誤處理纲爸,也是采用 try ...catch)


下面是最終解決錯(cuò)誤的代碼:

func getThunbImage(url: NSURL) -> (UIImage) { 
     let asset: AVURLAsset = AVURLAsset(URL: url, options: nil) 
     let gen: AVAssetImageGenerator = AVAssetImageGenerator(asset: asset) 
    gen.appliesPreferredTrackTransform = true 
    let time: CMTime = CMTimeMakeWithSeconds(0, 1) 
    var actualTime: CMTime = CMTimeMake(0, 0) 
    var thumb: UIImage = UIImage() 
      do { 
           let image: CGImageRef = try gen.copyCGImageAtTime(time, actualTime: &actualTime) 
           thumb = UIImage(CGImage: image) 
      } catch { }
         return thumb 
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市妆够,隨后出現(xiàn)的幾起案子识啦,更是在濱河造成了極大的恐慌,老刑警劉巖神妹,帶你破解...
    沈念sama閱讀 221,406評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件颓哮,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡鸵荠,警方通過查閱死者的電腦和手機(jī)冕茅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,395評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蛹找,“玉大人姨伤,你說我怎么就攤上這事∮辜玻” “怎么了乍楚?”我有些...
    開封第一講書人閱讀 167,815評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長彼硫。 經(jīng)常有香客問我炊豪,道長,這世上最難降的妖魔是什么拧篮? 我笑而不...
    開封第一講書人閱讀 59,537評(píng)論 1 296
  • 正文 為了忘掉前任词渤,我火速辦了婚禮,結(jié)果婚禮上串绩,老公的妹妹穿的比我還像新娘缺虐。我一直安慰自己,他們只是感情好礁凡,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,536評(píng)論 6 397
  • 文/花漫 我一把揭開白布高氮。 她就那樣靜靜地躺著慧妄,像睡著了一般。 火紅的嫁衣襯著肌膚如雪剪芍。 梳的紋絲不亂的頭發(fā)上塞淹,一...
    開封第一講書人閱讀 52,184評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音罪裹,去河邊找鬼饱普。 笑死,一個(gè)胖子當(dāng)著我的面吹牛状共,可吹牛的內(nèi)容都是我干的套耕。 我是一名探鬼主播,決...
    沈念sama閱讀 40,776評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼峡继,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼冯袍!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起碾牌,我...
    開封第一講書人閱讀 39,668評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤康愤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后舶吗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體翘瓮,經(jīng)...
    沈念sama閱讀 46,212評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,299評(píng)論 3 340
  • 正文 我和宋清朗相戀三年裤翩,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了资盅。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,438評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡踊赠,死狀恐怖呵扛,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情筐带,我是刑警寧澤今穿,帶...
    沈念sama閱讀 36,128評(píng)論 5 349
  • 正文 年R本政府宣布,位于F島的核電站伦籍,受9級(jí)特大地震影響蓝晒,放射性物質(zhì)發(fā)生泄漏形用。R本人自食惡果不足惜抡四,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,807評(píng)論 3 333
  • 文/蒙蒙 一族奢、第九天 我趴在偏房一處隱蔽的房頂上張望责循。 院中可真熱鬧,春花似錦饭于、人聲如沸侨歉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,279評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽晾嘶。三九已至妓雾,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間垒迂,已是汗流浹背械姻。 一陣腳步聲響...
    開封第一講書人閱讀 33,395評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留机断,地道東北人策添。 一個(gè)月前我還...
    沈念sama閱讀 48,827評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像毫缆,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子乐导,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,446評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容