1

我正在学习如何以编程方式创建 UICollectionView。我想在应用程序的另一部分创建从用户那里收集的图片网格。这个示例代码能帮我完成这个吗?另外,如何配置数据以发出我想要的图像?我的源代码如下。

UICollectionView:

class PhotosViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    override func viewDidLoad() {
        super.viewDidLoad()
        let imageStore = ImageStore()
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: 100, height: 100)

        let myCollectionView:UICollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        myCollectionView.dataSource = self
        myCollectionView.delegate = self
        myCollectionView.registerClass(RDCellCollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
        myCollectionView.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(myCollectionView)
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count
    }

    var images: [UIImage] = [

    ]

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let myCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! RDCellCollectionViewCell
        myCell.imageView.image = images[indexPath.item]
        myCell.backgroundColor = UIColor.grayColor()
        return myCell
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
    {
        print("User tapped on item \(indexPath.row)")
    }
}

ImageStore.swift:

class ImageStore: NSObject {

    let cache = NSCache()

    func setImage(image: UIImage, forKey key: String) {
        cache.setObject(image, forKey: key)

        let imageURL = imageURLForKey(key)

        if let data = UIImageJPEGRepresentation(image, 0.5) {
            data.writeToURL(imageURL, atomically: true)
        }
    }
    func imageForKey(key: String) -> UIImage? {
        if let existingImage = cache.objectForKey(key) as? UIImage {
            return existingImage
        }

        let imageURL = imageURLForKey(key)
        guard let imageFromDisk = UIImage(contentsOfFile: imageURL.path!) else {
            return nil
        }

        cache.setObject(imageFromDisk, forKey: key)
        return imageFromDisk
    }

    func deleteImageForKey(key: String) {
        cache.removeObjectForKey(key)

        let imageURL = imageURLForKey(key)
        do {
            try NSFileManager.defaultManager().removeItemAtURL(imageURL)
        }
        catch let deleteError {
            print("Error removing the image from disk: \(deleteError)")
        }
    }

    func imageURLForKey(key: String) -> NSURL {
        let documentsDirectories =
        NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
        let documentDirectory = documentsDirectories.first!

        return documentDirectory.URLByAppendingPathComponent(key)
    }
}
4

2 回答 2

0

你在正确的轨道上。您需要创建一个包含 UIImageView 的 UICollectionViewCell 子类;这将让您在 cellForItemAtIndexPath 中插入正确的 UIImage。

这描述了如何连接您的自定义单元格: Create UICollectionViewCell programmatically without nib or storyboard

至于获取正确的图像,您需要以某种方式将索引路径映射到图像存储,以便项目编号对应于正确的图像键。

于 2016-11-10T15:28:42.890 回答
0

如果任务是添加图像,您应该在 cellForItemAtIndexPath 中使用类似的内容:

let myCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath)
myCell.backgroundColor = UIColor.blueColor()
let imageView = UIImageView(frame: cell.contentView.frame)
cell.contentView.addSubview(imageView)
imageView.image = //Here you should get right UIImage like ImageStore().imageForKey("YOUR_KEY")
return myCell

或者您可以使用 Joshua Kaden 所写的自定义 UICollectionViewCell 子类。

于 2016-11-10T16:35:18.070 回答