Tusker/Tusker/Views/Asset Picker/AlbumTableViewCell.swift

43 lines
1.3 KiB
Swift

//
// AlbumTableViewCell.swift
// Tusker
//
// Created by Shadowfacts on 1/4/20.
// Copyright © 2020 Shadowfacts. All rights reserved.
//
import UIKit
import Photos
class AlbumTableViewCell: UITableViewCell {
@IBOutlet weak var thumbnailImageView: UIImageView!
@IBOutlet weak var albumTitleLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
thumbnailImageView.layer.masksToBounds = true
thumbnailImageView.layer.cornerRadius = 0.05 * thumbnailImageView.bounds.width
}
func updateUI(album: PHAssetCollection) {
albumTitleLabel.text = album.localizedTitle
let options = PHFetchOptions()
options.fetchLimit = 1
options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let fetchResult = PHAsset.fetchAssets(in: album, options: options)
if fetchResult.count == 1 {
let asset = fetchResult.object(at: 0)
let size = thumbnailImageView.bounds.size
PHImageManager.default().requestImage(for: asset, targetSize: size, contentMode: .aspectFill, options: nil) { (image, _) in
self.thumbnailImageView.image = image
}
} else {
thumbnailImageView.image = nil
}
}
}