设置UImage表单本地路径URL - 导致崩溃

时间:2017-07-16 11:10:08

标签: ios iphone swift uiimage ios10

使用本地路径网址在集合视图中设置图像视图,如下所示:

let fileURL = documentsUrl.appendingPathComponent(fileName)
myImageView.image  =  UIImage(named: fileURL.path)

导致崩溃 - 图像质量很高 - 试图减小尺寸 - 仍然会在没有错误消息的情况下崩溃应用程序。

2 个答案:

答案 0 :(得分:0)

func getDirectoryPath() -> String {
    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

let imagePath = (getDirectoryPath() as NSString).appendingPathComponent("/\(file_name).jpg")

let myImageView.image = UIImage(contentsOfFile: imagePath)!

答案 1 :(得分:0)

如果图片在捆绑包中(包含在应用中),则不要将整个路径传递给UIImage(named: ...),只传递文件名。因此,您的通话将为UIImage(named: fileName)

如果您在文档路径中有图像(您是从应用程序下载的),请使用UIImage(contentsOfFile: fileURL.path)

相关问题