Shared iCloud album returns all assets slowly

13 Views Asked by At

Created async function that, once user has given permission and selected shared album, runs to gather all assets in a shared album. It is slow and I'm not sure what I am doing wrong.

struct AlbumAsset: Identifiable {
    
    let id = UUID().uuidString
    let index: Int
    let album: String
    let file: String
    let name: String
    let image: Image
    let asset: assetType
    let imageType: PHAssetMediaType
    let imageWidth: Int
    let imageHeight: Int
    let createDate: Date
    let location: CLLocationCoordinate2D
    
    init(id: String = UUID().uuidString, index: Int, album: String, file: String, name: String, createDate: Date, asset: assetType, image: Image, imageType: PHAssetMediaType, imageWidth: Int, imageHeight: Int, location: CLLocationCoordinate2D){
        self.index = index
        self.album = album
        self.file = file
        self.name = name
        self.createDate = createDate
        self.asset = asset
        self.image = image
        self.imageType = imageType
        self.imageWidth = imageWidth
        self.imageHeight = imageHeight
        self.location = location
    }
    
}

func getAlbumAssets(albumName: String) async -> Array<AlbumAsset> {
        
    let settings = appSettings()

    var albumAssets: [AlbumAsset] = [AlbumAsset]()
    var albumAssetsResult = PHFetchResult<AnyObject>()
        
    let fetchOptions = PHFetchOptions()
    fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)
    
    let albumCollection:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
    if albumCollection.firstObject != nil {
    
        albumAssetsResult = PHAsset.fetchAssets(in: albumCollection.firstObject!, options: nil) as! PHFetchResult<AnyObject>
        
        let requestImageOptions = PHImageRequestOptions()
        requestImageOptions.version = .current // .original // .unadjusted
        requestImageOptions.resizeMode = .exact : .fast // .none
        requestImageOptions.deliveryMode = .highQualityFormat // .opportunistic // .opportunistic //fastFormat
        requestImageOptions.isNetworkAccessAllowed = true
        requestImageOptions.isSynchronous = true

        let requestVideoOptions = PHVideoRequestOptions()
        requestVideoOptions.version = .current // .original
        requestVideoOptions.deliveryMode = .highQualityFormat // .mediumQualityFormat // .fastFormat // .automatic
        requestVideoOptions.isNetworkAccessAllowed = true
        
        let imageManager =  PHImageManager() // PHCachingImageManager()

        var imageRequestWidth = 200
        var imageRequestHeight = 200
        let imageSize = CGSize(width:imageRequestWidth,height:imageRequestHeight) // PHImageManagerMaximumSize 
        
        var albumAssetsRecords: [PHAsset] = []
        albumAssetsResult.enumerateObjects{(object: AnyObject!, count: Int, stop: UnsafeMutablePointer<ObjCBool>) in
                
                if object is PHAsset{
                    let asset = object as! PHAsset
                    
                    albumAssetsRecords.append(asset)
                    
                    var mediaimage = asset.mediaType == .image ? Image("iva") : Image("vai")
                                        
                    let index       = count
                    let album       = albumName
                    let file        = asset.fileName
                    let name        = asset.localIdentifier
                    let cdate       = asset.creationDate
                    let mediaasset  = assetType.image(type: "")
                    let imageType   = asset.mediaType
                    let imageWidth  = Int(CGFloat(asset.pixelWidth))
                    let imageHeight = Int(CGFloat(asset.pixelHeight))
                    let location    = CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)

                    if asset.pixelWidth > imageRequestWidth && asset.pixelHeight > imageRequestHeight { //found that some photos were zero in size
                        imageManager.requestImage(for: asset, targetSize: imageSize, contentMode: .aspectFit, options: requestImageOptions, resultHandler: { (image, info) in
                            if image != nil {
                                mediaimage = Image(uiImage: image!)
                            }
                        })
                    }
                    
                    let newAsset = AlbumAsset(index: index, album: album, file: file, name: name, createDate: cdate!, asset: mediaasset, image: mediaimage, imageType: imageType, imageWidth: imageWidth, imageHeight: imageHeight, location: location)
                    albumAssets.append(newAsset)
                    
                }
                
            }
            
            return albumAssets
        }
        
    }
    return []
}

Return of array has multiple attributes which are used in LIST or other foreach on view.

Other function uses similar but only pulls collection of shared albums.

0

There are 0 best solutions below