Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 动态使用多维数组作为UIImage数组_Ios_Arrays_Swift_Uiimage - Fatal编程技术网

Ios 动态使用多维数组作为UIImage数组

Ios 动态使用多维数组作为UIImage数组,ios,arrays,swift,uiimage,Ios,Arrays,Swift,Uiimage,我正在开发一个应用程序,在滚动视图中制作分页照片,就像在照片应用程序中一样,向右滑动以获取旧照片,向左滑动以获取新照片,直到照片结束 好的,我从网上得到了这些照片: imageArray[indexPath.row][0] as? String 在ViewController1中显示CollectionView中的所有图像 当用户按下照片时,我会在ViewController2中切换并显示较大的图像,因此如果向左滑动,则会显示新照片,向右滑动则会显示存储在阵列中的旧照片 但我需要将我的二维数组

我正在开发一个应用程序,在滚动视图中制作分页照片,就像在照片应用程序中一样,向右滑动以获取旧照片,向左滑动以获取新照片,直到照片结束

好的,我从网上得到了这些照片:

imageArray[indexPath.row][0] as? String
ViewController1
中显示CollectionView中的所有图像

当用户按下照片时,我会在
ViewController2
中切换并显示较大的图像,因此如果向左滑动,则会显示新照片,向右滑动则会显示存储在阵列中的旧照片

但我需要将我的二维数组转换为一个数组,并动态地使用它,使其类似于这样:

pageImages = [UIImage(named:"photo1.png")!,
    UIImage(named:"photo2.png")!,
    UIImage(named:"photo3.png")!,
    UIImage(named:"photo4.png")!,
    UIImage(named:"photo5.png")!]
怎么可能呢

我可以这样说:

pageImages=[UIimage(命名为thewholearray)]

我首先尝试转换为一个小型数组,但失败了:

var imageArray : NSArray = []

var mybigarray : NSArray = []
    for (var i=0; i<=self.imageArray.count; i++) {

        self.mybigarray = self.imageArray[i][0] as! NSArray

    }

可以使用map函数从多维数组中提取图像名称

var imageNames:[String] = imageArray.map({
    $0[0] as! String
})
map函数像for in循环一样遍历所有数组项。 闭包中的语句确定imageNames数组的新条目

编辑:

如果不使用Swift数组:

var imageNames:NSMutableArray = []
for image in imageArray{
    imageNames.addObject(image[0] as! String)
}

我看了教程,我认为这将有助于丹尼尔的答案。保留Daniel的答案,并使用扩展名数组中的URL。
添加此项可从URL创建图像

extension UIImageView {
    public func imageFromUrl(urlString: String) {
        if let url = NSURL(string: urlString) {
            let request = NSURLRequest(URL: url)
            NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {
                (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
                if data != nil {
                    self.image = UIImage(data: data)
                }
                else {
                    self.image = UIImage()
                }
            }
        }
    }
}
在PagedImageScrollViewController中使用此选项:

newPageView.imageFromUrl(pageImages[page])

pageImages是您的字符串数组。

NSArray没有名为map的成员?太好了,如果我需要将imageNames用作UIImage数组作为我的问题,最后一件事是如何做?imageNames.addObject(UIImage(名为:“\(image[0]as!String)”)!@AaoIi,我写的将是图像。我不明白您还想要什么。请澄清您的问题确定我会保留它,我正在使用SDWebImage library显示图像,因此,如果没有扩展名,怎么可能这样做,我试着像
让newPageView=UIImageView(image:pageImages[page]as!UIImage)newPageView.sd\u setImageWithURL(url)
但是它说
无法将类型为“\u NSCFString”(0x196806958)的值强制转换为“UIImage”(0x19746a698)。
对不起,我不熟悉SDWebImage库。出现此错误的原因可能是您的页面图像是一个字符串数组,并且您正在尝试将字符串设置为图像。如果您找不到解决方案,我可以稍后查看SDWebImage库。也许你可以操纵扩展来满足你的需要?
newPageView.imageFromUrl(pageImages[page])