简体   繁体   中英

How to insert solid color images into photo library collectionview

I already implemented code which shows photo library images in my UICollectionView but I really want my 6th 7th and 12th cell to be solid color. Is it possible to achieve? Basically I need somehow to insert cells and don't broke my code which inserts images from photo library.

Like this: 在此处输入图像描述

My code:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photocell", for: indexPath) as! AlbumCollectionViewCell
        cell.image.layer.cornerRadius = 5
        cell.image.clipsToBounds = true
        let asset = images[indexPath.row]
        let manager = PHImageManager.default()
        if cell.tag != 0 {
                manager.cancelImageRequest(PHImageRequestID(cell.tag))
            }
        cell.tag = Int(manager.requestImage(for: asset,
                                            targetSize: CGSize(width: 250.0, height: 250.0),
                                                contentMode: .aspectFill,
                                                options: nil) { (result, _) in
                                                    cell.image.image = result

            })
        return cell
}

I also reverse images array:

func getImages() {
    let assets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: nil)
    assets.enumerateObjects({ (object, count, stop) in
       // self.cameraAssets.add(object)
        self.images.append(object)
    })

    //In order to get latest image first, we just reverse the array
    self.images.reverse()

    // To show photos, I have taken a UICollectionView
    self.photosCollectionView.reloadData()
}

The thing is, you are allowed to have more than one type of cell. So you can have some cells that display images and other cells that display a solid swatch of color. So based on what item this is, instead of saying collectionView.dequeueReusableCell(withReuseIdentifier: "photocell" , you would say collectionView.dequeueReusableCell(withReuseIdentifier: "swatchcell" and you'd configure the swatch instead of configuring the photo.

EDIT You added a comment:

I would like much more to insert new item into my array of images which is [PHAsset] data type

Oh, so now the problem is merely what the model for the data should be. That's simple: just use a data type which is an Asset-Or-Color, and make an array of that:

enum AssetOrColor {
    case asset(PHAsset)
    case color(UIColor)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM