美文网首页
APP目录内的资源读取

APP目录内的资源读取

作者: 竹菜板 | 来源:发表于2017-03-25 10:19 被阅读18次

    图片

    let imageName = "image_name"
    let imageType = "png"
    let directory = "Data/images"
    
    func getImage() -> UIImage {
        let path = Bundle.main.path(forResource: imageName, ofType: imageType, inDirectory: directory)!
        return UIImage(contentsOfFile: path)!
    }
    
    

    JSON

    let jsonName = "json_name"
    let extensionName = "json"
    let directory = "Data/json"
    
    func getJsonString() -> String? {
        let url = Bundle.main.url(forResource: jsonName, withExtension: extensionName, subdirectory: directory)
        do {
            let jsonString = try String(data: Data(contentsOf: url!), encoding: .utf8)!
            return jsonString
        } catch {
            return nil
        }
    }
    
    

    相关文章

      网友评论

          本文标题:APP目录内的资源读取

          本文链接:https://www.haomeiwen.com/subject/lvzqottx.html