Ios 照片库资产';s mediaType在Swift中有时不准确
在Swift中编写多图像选择器。步骤如下:Ios 照片库资产';s mediaType在Swift中有时不准确,ios,swift,Ios,Swift,在Swift中编写多图像选择器。步骤如下: 从照片库获取所有资源 在UICollectionView 对于视频,在单元格右下角贴上一个小标签,上面写着“视频” 以下是我的代码: class PhotoLibraryViewController: UICollectionViewController { private class PhotoLibraryAsset { var assetType: String // Either "Video" or "P
UICollectionView
class PhotoLibraryViewController: UICollectionViewController {
private class PhotoLibraryAsset {
var assetType: String // Either "Video" or "Photo"
var thumbnail: UIImage
init(assetType: String, thumbnail: UIImage) {
self.assetType = assetType
self.thumbnail = thumbnail
}
}
var photos: [PhotoLibraryAsset]()
override func viewDidLoad() {
super.viewDidLoad()
getAllPhotos()
}
private func getAllPhotos() {
// Grab all photos & videos instead of just photos
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [
NSSortDescriptor.init(
key: "creationDate",
ascending: false
)
]
fetchOptions.predicate = NSPredicate.init(
format: "mediaType = %d || mediaType = %d",
PHAssetMediaType.image.rawValue,
PHAssetMediaType.video.rawValue
)
fetchOptions.fetchLimit = Int.max
let imageManager = PHImageManager()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
let fetchResult = PHAsset.fetchAssets(with: fetchOptions)
for i in 0..<result.count {
let asset = result.object(at: i)
// Now generate thumbnails for the assets
imageManager.requestImage(
for: asset,
targetSize: ...,
contentMode: .aspectFill,
options: requestOptions,
resultHandler: { (image, error) in
if let photo = image {
self.photos.append(
PhotoLibraryAsset.init(
asset.mediaType == .video ? "Video" : "Photo",
thumbnail: image!
)
)
}
}
)
}
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
...
let photo = photos[indexPath.row]
if photo.assetType == "Video" {
// Add a "Video" label to the bottom-right corner of the cell
}
...
}
}
class PhotoLibraryViewController:UICollectionViewController{
私有类照片库资产{
var assetType:String//“视频”或“照片”
var缩略图:UIImage
初始化(资产类型:字符串,缩略图:UIImage){
self.assetType=assetType
self.thumbnail=缩略图
}
}
var照片:[照片库资产]()
重写func viewDidLoad(){
super.viewDidLoad()
getAllPhotos()
}
私有func getAllPhotos(){
//抓取所有照片和视频,而不仅仅是照片
让fetchOptions=PHFetchOptions()
fetchOptions.sortDescriptors=[
NSSortDescriptor.init(
键:“creationDate”,
升序:假
)
]
fetchOptions.predicate=NSPredicate.init(
格式:“mediaType=%d | | mediaType=%d”,
PHAssetMediaType.image.rawValue,
PHAssetMediaType.video.rawValue
)
fetchOptions.fetchLimit=Int.max
让imageManager=PHImageManager()
让requestOptions=PHImageRequestOptions()
requestOptions.isSynchronous=true
让fetchResult=PHAsset.fetchAssets(带:fetchOptions)
对于0..UICollectionViewCell中的i{
...
let photo=photos[indexPath.row]
如果photo.assetType==“视频”{
//将“视频”标签添加到单元格的右下角
}
...
}
}
问题
有些照片的右下角也有“视频”标签,为什么?另外,在关闭屏幕并重新打开后,有些带有“视频”标签的照片现在已经没有了,但是有些照片现在没有了。我该怎么办
更新
这就是选择器的外观,在所有标记为“视频”的照片中,实际上只有一张是视频:
我想问题是您正在
单元格中创建并添加UILabel
,因为单元格将被重复使用,因此视频标签将随机出现在这些单元格中。我建议仅从故事板或XIB添加标签。或者您也可以在awakeFromNib
中创建并添加标签然后,您必须实现prepareforeuse
并将标签隐藏在那里。您是否在cellForRowAt:
委托中创建并添加UILabel
?是的,这是正确的通常是因为您忘记了单元格是重复使用的。