问题:我正在使用通过苹果 API(所有本地)访问的音乐中的艺术资产来创建一个 4 图像拼贴作为单元格中的图像,用于表格视图中的播放列表。我已经尝试了一些方法,我将通过这些方法将性能提高到可以接受的水平,但总会有一些问题。
方法 1)运行处理代码以返回每个单元格的拼贴图像。这对于我设备上的少数播放列表来说总是很好,但我收到重度用户的报告,他们无法滚动。
方法 2)在 viewdidload() 中,遍历所有播放列表并将拼贴图像和其中一个 UUID 保存在可变数组中,然后使用 UUID 获取单元格的图像。这似乎可以正常加载视图,但由于某种原因,后续加载需要更长的时间。
因此,在我尝试对方法 2 进行更多故障排除之前,我想知道我是否以完全错误的方式直接解决这个问题?我已经研究过 GCD 和 NSCache,就 GCD 而言,我还没有足够的知识来制定适当的设计模式来利用它,如果它甚至可能的话,因为 UI 更新和存储访问之类的东西可能会进入道路。
import UIKit
import MediaPlayer
class playlists: UITableViewController, UISearchBarDelegate, UISearchControllerDelegate {
...
var compositedCellImages:[(UIImage, UInt64)] = []
...
override func viewDidLoad() {
super.viewDidLoad()
let cloudFilter:MPMediaPropertyPredicate = MPMediaPropertyPredicate(value: false, forProperty: MPMediaItemPropertyIsCloudItem, comparisonType: MPMediaPredicateComparison.equalTo)
playlistsQuery.addFilterPredicate(cloudFilter)
playlistQueryCollections = playlistsQuery.collections?.filter{$0.value(forProperty: MPMediaPlaylistPropertyName) as? String != "Purchased"} as NSArray?
var tmpArray:[MPMediaPlaylist] = []
playlists = playlistQueryCollections as! [MPMediaPlaylist]
for playlist in playlists {
if playlist.value(forProperty: "parentPersistentID") as! NSNumber! == playlistFolderID {
tmpArray.append(playlist)
compositedCellImages.append(playlistListImage(inputPlaylistID: playlist.persistentID))
}
}
playlists = tmpArray
...
}
// MARK: - Table view data source
...
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "playlistCell", for: indexPath) as! playlistCell
...
let currentItem = playlists[indexPath.row]
...
cell.playlistCellImage.image = compositedCellImages[indexPath.row].0
}
}
return cell
}
...
func playlistListImage(inputPlaylistID:MPMediaEntityPersistentID) -> (UIImage,UInt64) {
var playlistData:[MPMediaItem] = []
var pickedArtwork:[UIImage] = []
var shuffledIndexes:[Int] = []
let playlistDetailImage:collageImageView = collageImageView()
playlistDetailImage.frame = CGRect(x: 0, y: 0, width: 128, height: 128)
let playlistDataPredicate = MPMediaPropertyPredicate(value: NSNumber(value: inputPlaylistID as UInt64), forProperty: MPMediaPlaylistPropertyPersistentID, comparisonType:MPMediaPredicateComparison.equalTo)
let playlistDataQuery = MPMediaQuery.playlists()
let cloudFilter:MPMediaPropertyPredicate = MPMediaPropertyPredicate(value: false, forProperty: MPMediaItemPropertyIsCloudItem, comparisonType: MPMediaPredicateComparison.equalTo)
playlistDataQuery.addFilterPredicate(cloudFilter)
playlistDataQuery.addFilterPredicate(playlistDataPredicate)
playlistData = playlistDataQuery.items!
playlistData = playlistData.filter{$0.mediaType == MPMediaType.music}
for (index,_) in playlistData.enumerated() {
shuffledIndexes.append(index)
}
shuffledIndexes.shuffleInPlace()
for (_,element) in shuffledIndexes.enumerated() {
if playlistData[element].artwork != nil {
pickedArtwork.append(playlistData[element].artwork!.image(at: CGSize(width: 64, height: 64))!)
}
if pickedArtwork.count == 4 { break }
}
while pickedArtwork.count < 4 {
if pickedArtwork.count == 0 {
pickedArtwork.append(UIImage(named: "missing")!)
} else {
pickedArtwork.shuffleInPlace()
pickedArtwork.append(pickedArtwork[0])
}
}
pickedArtwork.shuffleInPlace()
playlistDetailImage.drawInContext(pickedArtwork, matrixSize: 2)
return ((playlistDetailImage.image)!,inputPlaylistID)
}
...
}
...
class collageImageView: UIImageView {
var inputImages:[UIImage] = []
var rows:Int = 1
var cols:Int = 1
func drawInContext(_ imageSet: [UIImage], matrixSize: Int) {
let frameLeg:Int = Int(self.frame.width/CGFloat(matrixSize))
var increment:Int = 0
UIGraphicsBeginImageContextWithOptions(self.frame.size, false, UIScreen.main.scale)
self.image?.draw(in: self.frame)
for col in 1...matrixSize {
for row in 1...matrixSize {
imageSet[increment].draw(in: CGRect(x: (row - 1) * frameLeg, y: (col-1) * frameLeg, width: frameLeg, height: frameLeg))
increment += 1
}
}
self.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
}