简体   繁体   中英

Trying to retrieve a set of photos using ALAssetsLibrary

I am currently developing an iPhone application that implements face detection and tagging. I am using the SQLite database to store the tags and the url of corresponding images. Now, while retrieving I will be implementing some logic to filter out the desired images based on the tag and getting the set of URLs of images(from the Db) that match this tag. (Asset Library URL's of the form - assets-library://asset/asset.JPG?id=79465E8C-53B9-40D6-B11C-07A1856E9093&ext=JPG)

My question is, if I have an array of NSURL's, how do I load a custom image picker using ALAssetsLibrary with only the URL's present in the array and not all the images from the default photo library?

I have read how to load an image from a URL based on this answer: https://stackoverflow.com/a/18888938

for this question: display image from URL retrieved from ALAsset in iPhone

How do I run a single loop over my array of URL's to load these images using ALAssets into a custom UICollectionView to replicate the ImagePickerController?

看一下苹果提供的示例代码 ,以实现自定义图像选择器。

/**
 *  This method is used to get all images from myAlbum 
 *  folder in device if getAlbumImages is set to 1
 *  else loads all images from devices library
 */
-(void)readImages:(int)getAlbumImages
{
    imageArray = [[NSArray alloc] init];
    mutableArray =[[NSMutableArray alloc]init];
    NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];

    library = [[ALAssetsLibrary alloc] init];

    if(getFolderImages == 1) {
        // Load images from Shareaflash folder
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
        NSString *documentdataPath = [documentsDirectory stringByAppendingPathComponent:@"myFolder"];
        NSLog(@"documentdataPath %@",documentdataPath);

    } else {
        // Load all images
    }

    void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if(result != nil) {
            if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];

                NSURL *url = (NSURL*) [[result defaultRepresentation]url];

                [library assetForURL:url resultBlock:^(ALAsset *asset) {
                             [mutableArray addObject:asset];

                             if ([mutableArray count]==count)
                             {
                                 imageArray =[[NSArray alloc] initWithArray:mutableArray];
                                 [self allPhotosCollected:imageArray];
                             }
                         }
                        failureBlock:^(NSError *error){ NSLog(@"operation was not successfull!"); } ];
            }
        }
    };
    NSMutableArray *assetGroups = [[NSMutableArray alloc] init];

    void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
        if(group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
            [assetGroups addObject:group];
            count=[group numberOfAssets];
        }
    };
    assetGroups = [[NSMutableArray alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupAll
                         usingBlock:assetGroupEnumerator
                         failureBlock:^(NSError *error) { NSLog(@"There is an error"); }];
}

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