最近使用Swift编程中,遇到一个问题,就是出现了Call can throw, but it is not marked with ‘try’ and the error is not handled的错误。
我做的是获取视频的首帧图片,在使用到 copyCGImageAtTime(requestedTime: CMTime, actualTime: UnsafeMutablePointer<CMTime>) throws -> CGImage 方法时,出现了Call can throw, but it is not marked with ‘try’ and the error is not handled的错误。刚开始以为是参数的错误,因为在OC该方法是这样的- (nullable CGImageRef)copyCGImageAtTime:(CMTime)requestedTime actualTime:(nullable CMTime *)actualTime error:(NSError * __nullable * __nullable)outError,纠结好久,上网查阅相关资料,解决了!下面看一下:

这是OC中方法的解释:

/*!@method         copyCGImageAtTime:actualTime:error:@abstract       Returns a CFRetained CGImageRef for an asset at or near the specified time.@param          requestedTimeThe time at which the image of the asset is to be created.@param          actualTimeA pointer to a CMTime to receive the time at which the image was actually generated. If you are not interestedin this information, pass NULL.@param          outErrorAn 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.
*/- (nullable CGImageRef)copyCGImageAtTime:(CMTime)requestedTime actualTime:(nullable CMTime *)actualTime error:(NSError * __nullable * __nullable)outError CF_RETURNS_RETAINED;

这是Swift中方法的解释

/*!@method         copyCGImageAtTime:actualTime:error:@abstract       Returns a CFRetained CGImageRef for an asset at or near the specified time.@param          requestedTimeThe time at which the image of the asset is to be created.@param          actualTimeA pointer to a CMTime to receive the time at which the image was actually generated. If you are not interestedin this information, pass NULL.@param          outErrorAn 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

这是我出现问题代码的截图:


解决办法:
编码出现error:Call can throw, but it is not marked with ‘try’ and the error is not handled,通过加一个try解决,原因就是没有处理错误 (PS: 就像Java中的异常错误处理,也是采用 try …catch)

最终解决错误的代码:

func getThunbImage(url: NSURL) -> (UIImage) {let asset: AVURLAsset = AVURLAsset(URL: url, options: nil)let gen: AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)gen.appliesPreferredTrackTransform = truelet 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}

swift编码出现Call can throw, but it is not marked with 'try' and the error is not handled错误的解决相关推荐

  1. Swift Call can throw, but it is not marked with 'try' and the error is not handled

    出现这种错误的原因就是没有处理错误 解决方法,在报错的方法外加上try catch,即 do{  try ..} catch {} 如下 do {try mananger.removeItem(atP ...

  2. 【iOS】Swift3:执行save()的时候出现:Call can throw, but it is not marked with 'try' and the error is not handl

    报错:Call can throw, but it is not marked with 'try' and the error is not handled. 因为没有使用try catch语句处理 ...

  3. Swift 编码规范

    Swift 编码规范 按大概的先后顺序,本文尝试做到以下几点: 增进精确,减少程序员犯错的可能 明确意图 减少冗余 减少关于美的争论 如果你有什么建议,请看我们的  贡献导引,然后开个  pull r ...

  4. Python编码错误的解决办法SyntaxError: Non-ASCII character ‘\xe5‘ in file

    Python编码错误的解决办法SyntaxError: Non-ASCII character '\xe5' in file  [现象] 在编写Python时,当使用中文输出或注释时运行脚本,会提示错 ...

  5. python2发送http不编码_[转]Python 2.x中常见字符编码和解码方面的错误及其解决办法...

    Python 2.x中的字符编码,设计的的确不好,导致初学者,甚至是即使用Python很长时间的人,都会经常遇到字符编解码方面的错误. 下面就把一些常见情,尽量的都整理出来,并给出相应的解决办法. 看 ...

  6. NSArray element failed to match the Swift Array Element type错误的解决方法

    在用swift调用yy_model生成的嵌套数组模型的时候回报错如下 错误的解决方法: Thread 1: Precondition failed: NSArray element failed to ...

  7. Python中编码问题:u'\xe6\x97\xa0\xe5\x90\x8d' 类型和 ‘\u559c\u6b22\u4e00\u4e2a\u4eba ’ 转为utf-8的解决办法...

    Python中编码问题:u'\xe6\x97\xa0\xe5\x90\x8d' 类型和 '\u559c\u6b22\u4e00\u4e2a\u4eba ' 转为utf-8的解决办法 相信小伙伴们遇到过 ...

  8. Python中编码问题:u‘\xe6\x97\xa0\xe5\x90\x8d‘ 类型和 ‘\u559c\u6b22\u4e00\u4e2a\u4eba ’ 转为utf-8的解决办法

    Python中编码问题:u'\xe6\x97\xa0\xe5\x90\x8d' 类型和 '\u559c\u6b22\u4e00\u4e2a\u4eba ' 转为utf-8的解决办法 参考文章: (1) ...

  9. php连接数据库404错误代码,访问编码后的中文URL返回404错误的解决方法,url404_PHP教程...

    访问编码后的中文URL返回404错误的解决方法,url404 昨天做一个项目,其中有一个需求是每一张图片对应一小段文字对图片的说明,普通的做法是新建一个表然后把图片名与说明文字都记录到数据库内.仔细考 ...

最新文章

  1. shell编程基础(2)---与||
  2. 投稿2877篇,EMNLP 2019公布4篇最佳论文
  3. python实现将txt文件格式转换为arff格式
  4. 去除警告: FutureWarning: In future, it will be treated as `np.float64 == np.dtype(float).type`.
  5. 2022年美国大学生数学建模竞赛——Problem E:林业固碳
  6. 如何在VSTFS中设置email notification
  7. controller配对与接触配对
  8. leetcode387. 字符串中的第一个唯一字符
  9. jQuery验证validate插件
  10. python编程入门单例_python实现单例模式怎么写啊?
  11. 语法高亮_Qt官方示例语法高亮器
  12. 思源黑体(魅族)、方正兰亭(小米)、冬青黑体(锤子)比较
  13. excel oss 上传_阿里云对象存储OSS全系统教程
  14. 嵌入式Linux之正点原子Linux开发板入手
  15. 【架构师考试】架构师考试内容与历年真题
  16. 使用密钥登录到ssh服务器
  17. Python列表推导式——List
  18. 2021安道拓企业研究数据报告_爱普搜汽车
  19. ipad的服务器是什么系统,ipad cellular版是什么意思 与WLAN版有什么区别
  20. 电机三角形接法和星形接法的理解

热门文章

  1. 看完以后就不会混淆啦,靠理解去区别substr, substring, slice,splice, split方法
  2. 计算机管理器打开出现找不到文件
  3. 2017年下半年自媒体的发展趋势如何?这几种那种你比较了解?...
  4. 阿里云性能测试工具 PTS 介绍
  5. 网站压力性能免费在线测试:阿里云PTS,Load Impact,loader.io服务器负载能力测试
  6. 蔡高厅高等数学22-导数的概念、两种表现形式
  7. 当TensorFlow遇上Kubernetes ---中兴通讯人工智能计算平台的技术实践
  8. 9月书单01-《别独自用餐》《赢》《联盟》《横向领导力》《梳毛、八卦语言的进化》《瘟疫与战争》
  9. 关于研究生英文论文写作的一点思考
  10. 大闸蟹提货系统asp版源码提供