如何在 Swift 3 中将图像从文档目录显示到 UIImageView? [英] How to display an image from documents directory to UIImageView in Swift 3?

查看:29
本文介绍了如何在 Swift 3 中将图像从文档目录显示到 UIImageView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的 Swift 2 示例给出了这个错误:

The below Swift 2 example gives this error:

String 类型的值没有成员 'stringByAppendingPathComponent'

Value of type String has no member 'stringByAppendingPathComponent'

我需要为 Swift 3 做哪些更改?

What do I need to change for Swift 3?

推荐答案

Apple 正试图将每个人从 path-as-string 范式转移到 URL(即 file:///path/to/file.text).Swift API 几乎删除了所有 path 以支持 URL.

Apple is trying to move everyone off the path-as-string paradigm to URL (i.e. file:///path/to/file.text). The Swift API pretty much removes all path in favor of URL.

你仍然可以在 Objective-C 中找到它(NSString):

You can still find it in Objective-C (NSString):

let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let getImagePath = NSString.path(withComponents: [paths, "fileName"])

更快捷的方式:

let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = URL(fileURLWithPath: paths).appendingPathComponent("fileName")

这篇关于如何在 Swift 3 中将图像从文档目录显示到 UIImageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆