从Swift的smartLibrary中的特定照片相册中选择所有照片

时间:2018-08-22 10:27:07

标签: swift phphotolibrary

当我从。相册获取照片时,代码可以正常工作

PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions) 

但是当我从 .smartAlbum 获取照片时,此代码不会返回照片

PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: fetchOptions)

这是我用来从smartAlbum提取照片的全部功能。

func FetchCustomAlbumPhotos(folderName:String)
{ 
    let albumName = folderName
    var assetCollection = PHAssetCollection()
    var albumFound = Bool()
    var photoAssets = PHFetchResult<AnyObject>()

    let fetchOptions = PHFetchOptions()
    fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)
    // fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)


    print("fetchOptions\(fetchOptions)")

    let collection:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: fetchOptions)

    if let first_Obj:AnyObject = collection.firstObject{
        //found the album
        assetCollection = (collection.firstObject)!
        albumFound = true
    }
    else { albumFound = false }

    photoAssets = PHAsset.fetchAssets(in: assetCollection, options: nil) as! PHFetchResult<AnyObject>
    let imageManager = PHCachingImageManager()

    //        let imageManager = PHImageManager.defaultManager()

    photoAssets.enumerateObjects{(object: AnyObject!,
        count: Int,
        stop: UnsafeMutablePointer<ObjCBool>) in

        if object is PHAsset{
            let asset = object as! PHAsset
            print("Inside  If object is PHAsset, This is number 1")

            let imageSize = CGSize(width: asset.pixelWidth,
                                   height: asset.pixelHeight)

            /* For faster performance, and maybe degraded image */
            let options = PHImageRequestOptions()
            options.deliveryMode = .fastFormat
            options.isSynchronous = true

            imageManager.requestImage(for: asset, targetSize: imageSize,contentMode: .aspectFill,options: options,resultHandler: {(image, info) -> Void in
                /* The image is now available to us */
                self.addImgToArray(uploadImage: image!)
                print("enum for image, This is number 2")

            })

        }
    }
}

0 个答案:

没有答案