Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法在swift 2中显示来自外部资源文件夹的jpeg_Ios_Swift_Uiimageview_Uiimage_Jpeg - Fatal编程技术网

Ios 无法在swift 2中显示来自外部资源文件夹的jpeg

Ios 无法在swift 2中显示来自外部资源文件夹的jpeg,ios,swift,uiimageview,uiimage,jpeg,Ios,Swift,Uiimageview,Uiimage,Jpeg,这可能是我无知的一个问题,但我只是想展示一个形象。。。按照下面的代码 我没有发现任何错误。图像存在,但不显示在模拟器中。 如果我将路径字符串更改为不存在的图像,我会得到一个错误,因此我假设它正在正确地查找图像。 我错过了什么 不要使用hospotimage=UIImageView(image:image!)尝试hospotimage.image=image像这样试试 let paths = NSSearchPathForDirectoriesInDomains(.DocumentDir

这可能是我无知的一个问题,但我只是想展示一个形象。。。按照下面的代码

我没有发现任何错误。图像存在,但不显示在模拟器中。 如果我将路径字符串更改为不存在的图像,我会得到一个错误,因此我假设它正在正确地查找图像。 我错过了什么


不要使用
hospotimage=UIImageView(image:image!)
尝试
hospotimage.image=image

像这样试试

    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
    let getImagePath = paths.stringByAppendingString("filename")
    testingImageView.image = UIImage(contentsOfFile: getImagePath)

您应该调用并给出文档目录的路径。。。试试下面的代码

let path = NSSearchPathForDirectoriesInDomains(
       .DocumentDirectory, .UserDomainMask, true)[0] as NSString
let fileName = NSUserDefaults.standardUserDefaults()
               .stringForKey("HI2.jpg") ?? defaultName
let imagePath = path.stringByAppendingPathComponent(fileName)!
let image = UIImage(contentsOfFile: imagePath )

// display the image
someImageView.image = image

您正在创建UIImageView,但没有将其添加到视图层次结构中,因此它不会显示在屏幕上。HotspotImage将已经是UIImageView,因此您应该将图像分配给其图像属性这发生在我的应用程序上。在Objective-C中,我们可以检索路径并动态显示工作的图像。(非常感谢)。你能告诉我我的版本有什么问题吗?
let path = NSSearchPathForDirectoriesInDomains(
       .DocumentDirectory, .UserDomainMask, true)[0] as NSString
let fileName = NSUserDefaults.standardUserDefaults()
               .stringForKey("HI2.jpg") ?? defaultName
let imagePath = path.stringByAppendingPathComponent(fileName)!
let image = UIImage(contentsOfFile: imagePath )

// display the image
someImageView.image = image