Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.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 在变量中设置图像时应用程序崩溃_Ios_Swift - Fatal编程技术网

Ios 在变量中设置图像时应用程序崩溃

Ios 在变量中设置图像时应用程序崩溃,ios,swift,Ios,Swift,我试图将图像存储在变量中,但我的应用程序在以下给定行中崩溃: let theImage = UIImage(named: self.arrImages[indexPath.row])! 这里,arrmages是一个包含图像的数组 我遇到的车祸是 致命错误:在展开可选值时意外发现nil 编辑1 根据@Duncan C和@Valentin的要求 声明: var arrImages = [String]() 在UICollectionView的委托中使用它 func collectionView(

我试图将图像存储在变量中,但我的应用程序在以下给定行中崩溃:

let theImage = UIImage(named: self.arrImages[indexPath.row])!
这里,
arrmages
是一个包含图像的数组

我遇到的车祸是

致命错误:在展开可选值时意外发现nil

编辑1 根据@Duncan C和@Valentin的要求 声明:

var arrImages = [String]()
UICollectionView的委托中使用它

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    if collectionView == self.collectionView
    {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! ImageCell

        cell.imageView.image = nil
        var theImage = UIImage()
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {

            if let image = UIImage(named: self.arrImages[indexPath.row]) {
                // Do whatever you like with the image here
                 theImage = UIImage(named: self.arrImages[indexPath.row])!
            }


            dispatch_async(dispatch_get_main_queue(), {

                cell.imageView.image =  theImage
                cell.imageView.contentMode = .ScaleAspectFit

            });
        })

        return cell
    }
试着把它和

if let image = UIImage(named: ...) {
    // Do whatever you like with the image here  
}

你没有提供足够的信息,你所说的一些话没有意义

发布ARR映像的声明,以及将值安装到数组中的代码

您说数组包含图像,但代码希望数组包含图像文件名(字符串)

接下来,您需要将代码分解为多个步骤并进行调试:

let index = indexPath.row
let imageName = self.arrImages[index];
println("At index \(index), imageName = \"\(imageName)\"")
let image = UIImage(named: imageName)
println("Image = \(image)")

值是否为正确的图像名称?@luk2302:是的,我也确认了这一点……然后尝试分割实际图像创建和展开,并查看构造函数返回的图像是否为零。@luk2302无法获取您的最后评论基本上删除
最后,查看图像的内容-与@Valentin建议的内容非常接近。让我试试,希望此帮助使用了您建议的内容,但之后我收到了以下错误:“malloc:**对象0x790b6400的错误:未分配要释放的指针***在malloc\u error\u break中设置断点以进行调试”你的代码有问题-你能发布你的整个类以便我们分析它吗?可选绑定(我的代码)基本上只接受UIImage(名为:…),如果它存在,则将其分配给image常量。