how to get image from PHAssetCollection

994 views Asked by At

As you see I try to retrieve image from all photos and put last one in imageView. I don't know how to do the same with selfie. How to use PHImageManager to retrieve image from selfie collection.

    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)")}
0

There are 0 answers