如您所见,我尝试从所有照片中检索图像并将最后一张放在imageView中。我不知道如何用自拍来做同样的事情。如何使用PHImageManager从自拍集中检索图像。
let photoOptions = PHFetchOptions()
photoOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
let photos = PHAsset.fetchAssets(with: .image, options: photoOptions)
let selfies = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumSelfPortraits, options: photoOptions)
var size = CGSize()
size.height = 200
size.width = 200
let asset = photos.lastObject
PHImageManager.default().requestImage(for: asset!, targetSize: size, contentMode: PHImageContentMode.aspectFit , options: nil) { (result, info) -> Void in
self.mainImage.image = result
print("P gI: Image gotten. Image Size: \(self.mainImage.image?.size), Size: \(size), Date: \(asset?.creationDate)")}
let selfieAsset = selfies.lastObject
PHImageManager.default().requestImage(for: selfieAsset!, targetSize: size, contentMode: PHImageContentMode.aspectFit , options: nil) { (result, info) -> Void in
self.mainImage.image = result
print("P gI: Image gotten. Image Size: \(self.mainImage.image?.size), Size: \(size), Date: \(asset?.creationDate)")}