アセット ライブラリを使用すると、アルバムのポスター画像を ALAssetsGroup から取得できます。Photos Framework (Photo kit) を使用して同じことを達成するにはどうすればよいですか?
質問する
9497 次
3 に答える
5
これが私が今使っているものです。さまざまな種類の写真コレクションと、重要なアセットが欠落している場合を処理します。
static func fetchThumbnail(collection: PHCollection, targetSize: CGSize, completion: @escaping (UIImage?) -> ()) {
func fetchAsset(asset: PHAsset, targetSize: CGSize, completion: @escaping (UIImage?) -> ()) {
let options = PHImageRequestOptions()
options.deliveryMode = PHImageRequestOptionsDeliveryMode.highQualityFormat
options.isSynchronous = false
options.isNetworkAccessAllowed = true
// We could use PHCachingImageManager for better performance here
PHImageManager.default().requestImage(for: asset, targetSize: targetSize, contentMode: .default, options: options, resultHandler: { (image, info) in
completion(image)
})
}
func fetchFirstImageThumbnail(collection: PHAssetCollection, targetSize: CGSize, completion: @escaping (UIImage?) -> ()) {
// We could sort by creation date here if we want
let assets = PHAsset.fetchAssets(in: collection, options: PHFetchOptions())
if let asset = assets.firstObject {
fetchAsset(asset: asset, targetSize: targetSize, completion: completion)
} else {
completion(nil)
}
}
if let collection = collection as? PHAssetCollection {
let assets = PHAsset.fetchKeyAssets(in: collection, options: PHFetchOptions())
if let keyAsset = assets?.firstObject {
fetchAsset(asset: keyAsset, targetSize: targetSize) { (image) in
if let image = image {
completion(image)
} else {
fetchFirstImageThumbnail(collection: collection, targetSize: targetSize, completion: completion)
}
}
} else {
fetchFirstImageThumbnail(collection: collection, targetSize: targetSize, completion: completion)
}
} else if let collection = collection as? PHCollectionList {
// For folders we get the first available thumbnail from sub-folders/albums
// possible improvement - make a "tile" thumbnail with 4 images
let inner = PHCollection.fetchCollections(in: collection, options: PHFetchOptions())
inner.enumerateObjects { (innerCollection, idx, stop) in
self.fetchThumbnail(collection: innerCollection, targetSize: targetSize, completion: { (image) in
if image != nil {
completion(image)
stop.pointee = true
} else if idx >= inner.count - 1 {
completion(nil)
}
})
}
} else {
// We shouldn't get here
completion(nil)
}
}
于 2018-05-31T10:31:42.933 に答える
1
それは非常に単純なことです...
PHFetchOptions *userAlbumsOptions = [PHFetchOptions new];
userAlbumsOptions.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount > 0"];
PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:userAlbumsOptions];
[userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
NSLog(@"album title %@", collection.localizedTitle);
PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
PHAsset *asset = [assetsFetchResult objectAtIndex:0];
NSInteger retinaMultiplier = [UIScreen mainScreen].scale;
CGSize retinaSquare = CGSizeMake(80 * retinaMultiplier, 80 * retinaMultiplier);
[[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:asset.localIdentifier] options:SDWebImageProgressiveDownload targetLocalAssetSize:retinaSquare progress:^(NSInteger receivedSize, NSInteger expectedSize) {
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (image) {
albumCoverImg.image = image;
}
}];
}];
SDWebImage クラスを更新していない場合は、通常の方法でイメージをロードします。
于 2015-01-27T08:21:15.500 に答える