简体   繁体   English

如何将图像存储到UIImage数组中(Swift)

[英]How do I store images into UIImage array (Swift)

Here is my code 这是我的代码

self.pages?.photo1.getDataInBackgroundWithBlock {
                (imageData: NSData?, error:NSError?) -> Void in
                if error == nil {
                    let image = UIImage(data: imageData!)
                    println(image!)
                    self.pageImages.append(image!)

when I run a println(pageImages.count) I get a 0 even though I know there are other elements in my dataset (pages) 当我运行println(pageImages.count)即使我知道数据集中还有其他元素(页面),我也会得到0。

NOTE: "photo1" is stored in a dictionary as a PFFIle type 注意:“ photo1”作为PFFIle类型存储在字典中

and a println(image) displays the following result 并且println(image)显示以下结果

UIImage: 0x7f90f3e12df0, {748, 420}} UIImage:0x7f90f3e12df0,{748、420}}

The problem is that you cannot add an element to an array the way you did. 问题是您不能像以前那样将元素添加到数组中。

To add an element at the end, use 要在最后添加元素,请使用

pageItems.append(image)

To insert an element at the beginning, use 要在开头插入元素,请使用

pageItems.insert(image, atIndex: 0)

The following works as expected in Playground: 以下在Playground中可以正常使用:

var imageArray: [UIImage] = []
let image = UIImage()
imageArray.append(image)
imageArray.insert(image, atIndex: 0)
let x = imageArray.count               // 2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Swift 3中将UIImage数组导出为电影? - How do I export UIImage array as a movie in Swift 3? 如何快速将图像数组传递给 UIImageView 类型的变量? 或者将多个图像存储在单个数组中的正确方法是什么? - How do I pass an array of images to a variable of type UIImageView in swift? Or what is the right way to store multiple images in a single array? 如何通过单击Swift 3中的按钮来更改另一个UIImage的UIImage? - How do I change a UIImage for another UIImage by clicking a button in Swift 3? 我如何在swift中拖动一个UIImage 4 - How do I drag a UIImage in swift 4 如何快速检查UIImage是否为空? - How do I check if UIImage is empty in swift? 如何将URL中的图像加载到UIImage数组中并在带有Swift的UIScrollView中使用它们 - How can I load Images from URL into an UIImage Array and use them in UIScrollView with Swift 如何在UIImage数组中设置第二个或中间图像? (快速3) - How to set the second or middle images in a UIImage array? (Swift 3) 如何在Swift中的UIImage数组中识别轻击的UIImage - How to Identify Tapped UIImage in UIImage array in Swift 如何使用数组中的字符串索引在 Swift 中使用 UIImage 与文本一起显示? - How do I use an index of strings in an array to display alongside text using UIImage in Swift? 如何编写一个可让所有动态单元格显示相同UIImage的快捷数组? - How do I write a swift array that can make the same UIImage appear for all my dynamic cells?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM