简体   繁体   中英

Check if PHAsset exists in PHFetchResult

how to know if asset for local identifier is not found. I have the list of localIDs of each photos and videos been fetched from the photos framework, how to know if the photo is present or not in the iOS photo album.

You need to keep track on the number of assets in that userAlbums and if you didn't find the asset until the last asset is checked return the Not Found notification.

You can do it like:

NSString *localId         = /*local identifier of photo */;
PHFetchResult *userAlbums = [PHAsset fetchAssetsWithLocalIdentifiers:@[localId] options:nil];
NSUInteger assetCount     = [userAlbums count];
__block NSUInteger assetCounter = 0;
[userAlbums enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop)
{
        // Check whether you found the asset or not
        if (/*asset found*/)
        {
           // Asset found, set the stop bool to yes
        }
        else if (assetCounter == assetCount)
        {
           //Data not found yet
        }
}];

Why not just use:

if ([userAlbums count]) {
    //At least one item found.
}
else {
    //Nothing found
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM