快速从本地获取图像

Vyk*_*tas 3 photo uiimageview swift

我真的很需要帮助

我将图像保存到 DocumentDirectory 如何获取此图像并放入 UIImageView?

照片网址:

file:///Users/zoop/Library/Developer/CoreSimulator/Devices/3E9FA5C0-3III-41D3-A6D7-A25FF3424351/data/Containers/Data/Application/7C4D9316-5EB7-4A70-82DC-E76C654sEA20profile

Mic*_*zyk 7

尝试这样的事情:

let fileName = "profileImage.png"
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! + "/" + fileName
let image = UIImage(contentsOfFile: path)
Run Code Online (Sandbox Code Playgroud)

然后,你可以把imageUIImageView

其他选项(如 Leo Dabus 在下面的评论中提到的):

let fileName = "profileImage.png"
let fileURL = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first!).URLByAppendingPathComponent(fileName)
if let imageData = NSData(contentsOfURL: fileURL) {
    let image = UIImage(data: imageData) // Here you can attach image to UIImageView
}
Run Code Online (Sandbox Code Playgroud)

  • 最简单的方法是使用 URLByAppendingPathComponent。顺便说一句,您应该开始习惯 NSURL 而不是常规字符串 (2认同)