Swift - 如何将图像保存到数组中并访问它们?

时间:2015-08-11 01:59:44

标签: ios arrays database swift

我有一张地图,允许我添加引脚(注释)。当我点击一个图钉时,该应用程序会转到另一个视图控制器并显示从在线下载的图像。我"保存"这些图像到一个数组但后来我无法访问它们。例如,当我点击"返回"转到上一个视图控制器并点击之前的相同引脚,而不是显示我最初下载和保存的图像,从在线下载新图像。基本上,阵列中的图像被替换。如何保存图像并检索它们?很抱歉代码很多,我尽可能地缩短了。

这是我的图片类:

class Image {

    var image: UIImage

    init(image: UIImage) {
        self.image = image
    }
}

这是我的针脚类。请注意,Image类中的Image类型数组位于:

class Pin: Hashable {

    var hashValue: Int {
        get {
            return "\(latitude.hashValue),\(longitude.hashValue)".hashValue
        }
    }

    let latitude: Double
    let longitude: Double
    var images = Array<Image>()

    init(latitude: Double, longitude: Double)
    {
        self.latitude = latitude
        self.longitude = longitude
    }
}

// must be declared in the global scope! and not just in the class scope
func ==(lhs: Pin, rhs: Pin) -> Bool
{
    return lhs.hashValue == rhs.hashValue
}

我将引脚添加到Set中(不允许重复引脚)。此外,所选引脚将发送到secondViewController:

class MapViewController: UIViewController, MKMapViewDelegate, UIGestureRecognizerDelegate {

var pins = Set<Pin>()

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
 // Add pin to set
            let selectedCoordinatePoint = Pin(latitude: latFromPin, longitude: lonFromPin)
            var select = "\(latFromPin.hashValue),\(lonFromPin.hashValue)".hashValue
            pins.insert(selectedCoordinatePoint)

        //Goto to next view controller and show data depending on the pin selected
        for pin in pins {
            if pin.hashValue == select.hashValue {

                dispatch_async(dispatch_get_main_queue()) {
                    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
                    let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("CollectionViewControllerID") as! CollectionViewController

                    secondViewController.pin = pin

                    self.navigationController?.pushViewController(secondViewController, animated: true)
                }
            }
        }
    }
}

我从在线下载图像并附加到第二个ViewController上的数组(我在这里缩短了代码):

class CollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, MKMapViewDelegate {

    var pin: Pin!

    override func viewWillAppear(animated: Bool) {


                if let photosArray = photosDictionary["photo"] as? [[String:AnyObject]] {
                    // println("photosArray  =  \(photosArray )")

                    var count = 0
                    for photo in photosArray {
                        // 6 - Grab 21 random images
                        if count <= 20 {
                            // Grabs 1 image
                            let randomPhotoIndex = Int(arc4random_uniform(UInt32(photosArray.count)))
                            let photoDictionary = photosArray[randomPhotoIndex] as [String:AnyObject]

                            // 7 - Get the image url
                            let imageUrlString = photoDictionary["url_m"] as? String
                            let imageURL = NSURL(string: imageUrlString!)

                            // 8 - If an image exists at the url, append to array
                            let imageData = NSData(contentsOfURL: imageURL!)
                            let finalImage = UIImage(data: imageData!)
                            var image = Image(image: finalImage!)
                            self.pin.images.append(image)
                            count += 1

                            println(self.pin.images.count)
                        }
                    }
                }
            }
}

1 个答案:

答案 0 :(得分:0)

我不明白你的确切要求......你正在进行网络调用以填充你的图像阵列。如果您只想将图像数组从VC传递到VC,则可以使用prepareForSegue传递该值。

如果你想将它们拉下来然后在本地存储,你需要阅读有关持久性的信息。例如,您可以使用Core Data,Parse,Realm ......我个人使用Parse因为它很容易。这是一个很好的教程,与我认为你正在做的事情一致。它使用Parse来存储图像数据:http://www.appcoda.com/instagram-app-parse-swift/