本文介绍了如何在ios7中使用ALAssetLibrary获取所有照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题像iphone8中的苹果iphone一样得到照片。我已经使用PHAsset和Photos.framework为ios8实现了。现在,当我在ios7设备中运行相同的代码时,它什么都不返回。所以,我和ALAssetLibrary一起去拍照。使用ALAssetLibrary我也得到了所有照片,但这就像专辑明智的照片。并且使用这个ALAssetLibrary我不能得到专辑创建日期而不是它的位置名称,因为我必须在我的每个部分的标题上显示这些数据。

I have problem to get photos in moment wise like apple iphone have in ios8. I have implemented for ios8 using PHAsset and Photos.framework. Now, when i run same code in ios7 device then it returns nothing. So, i go with ALAssetLibrary to get photos. Using ALAssetLibrary i also got all photos but that are like albums wise photos. and also using this ALAssetLibrary i cannot get album creation date not its location name, as i have to show this to data on my each section's header.

我使用ALAssetLibrary在ios​​7中获取照片的代码:

My code for fetching photos in ios7 using ALAssetLibrary:

        _imagearray = [@[] mutableCopy];
        __block NSMutableArray *tmpAssets = [@[] mutableCopy];

        ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
        [assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
         {
             if (group)
             {
                 [group setAssetsFilter:[ALAssetsFilter allAssets]];
                 [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
                     if (asset!=nil)
                     {
                         [tmpAssets addObject:asset];

                     }
                 }];
             }
             self.imagearray = tmpAssets;
             NSLog (@"%@",self.imagearray);
         }
                                  failureBlock:^(NSError *error)
         {
             NSLog(@"error enumerating AssetLibrary groups %@\n", error);
         }];


推荐答案

我找到了自己的解决方案。

I found its solutions my self.

+ (ALAssetsLibrary *)defaultAssetsLibrary {
    static dispatch_once_t pred = 0;
    static ALAssetsLibrary *library = nil;
    dispatch_once(&pred, ^{
        library = [[ALAssetsLibrary alloc] init];
    });
    return library;
}

-(void)loadAssets{
    NSMutableArray *unSortArray = [[NSMutableArray alloc] init];
    ALAssetsLibrary *library = [MomentsVCTR defaultAssetsLibrary];
    [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        if (group == nil) {
            NSLog(@"Done!");
            [self manageLocalAssets:unSortArray];
        }
        [group setAssetsFilter:[ALAssetsFilter allAssets]];
        [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
            if (alAsset) {
                [unSortArray addObject:alAsset];
            }

        }];
    } failureBlock: ^(NSError *error) {
        NSLog(@"No groups: %@",error);
    }];
}

-(void)manageLocalAssets:(NSMutableArray*)unSortArray{
    NSMutableArray *_resultArray = [[NSMutableArray alloc] init];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"dd-MMM-yyyy"];
    NSLog(@"in loadassets");
    NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
    NSArray *descriptors=[NSArray arrayWithObject: descriptor];
    NSArray *reverseOrder=[unSortArray sortedArrayUsingDescriptors:descriptors];

    for (int k=0; k<reverseOrder.count; k++) {
        ALAsset *asset = (ALAsset *)[reverseOrder objectAtIndex:k];
        NSString *dateStr = [df stringFromDate:[asset valueForProperty:ALAssetPropertyDate]];
        if (![self.arrDate containsObject:dateStr]) {
            [self.arrDate addObject:dateStr];
            [self.arrEventID addObject:@"0"];
            [self.arrEventName addObject:@"0"];
        }
        [_resultArray addObject:asset];
    }
    for (int i=0;i<self.arrDate.count;i++) {
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        NSMutableArray *arr2 = [[NSMutableArray alloc] init];
        int tPhoto = 0;
        int tVideo = 0;
        for (int j=0; j<_resultArray.count; j++) {
            ALAsset *asset = (ALAsset*)[_resultArray objectAtIndex:j];
            NSString *dateStr = [df stringFromDate:[asset valueForProperty:ALAssetPropertyDate]];
            if ([[self.arrDate objectAtIndex:i] isEqualToString:dateStr]) {
                UIImage *latestPhotoThumbnail = [UIImage imageWithCGImage:[asset thumbnail]];
                [arr addObject:latestPhotoThumbnail];
                latestPhotoThumbnail = nil;
                if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
                    [arr2 addObject:@"1"];
                    tVideo++;
                }
                else{
                    [arr2 addObject:@"0"];
                    tPhoto++;
                }
                NSDate *date = [asset valueForProperty:ALAssetPropertyDate];
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setLocale:[NSLocale currentLocale]];
                [dateFormatter setDateFormat:@"dd-MMM-yyyy"];
                [self.imageDateArray addObject:[dateFormatter stringFromDate:date]];
                [self.imageIdArray addObject:[NSString stringWithFormat:@"%d",i]];
            }
        }
        [self.imagearray addObject:arr];
        [self.arrContentType addObject:arr2];

        [self.momentArray addObject:[NSString stringWithFormat:@"%lu",(unsigned long)arr.count]];
        [self.arrPhotoCount addObject:[NSString stringWithFormat:@"%d",tPhoto]];
        [self.arrVideoCount addObject:[NSString stringWithFormat:@"%d",tVideo]];
    }
    [self setButtonsSize];
    self.collection.dataSource = self;
    self.collection.delegate = self;
    [self.collection reloadData];
    [self.collection.collectionViewLayout invalidateLayout];
    self.footerView.hidden = TRUE;
    self.footerWebView.hidden = TRUE;

}

这篇关于如何在ios7中使用ALAssetLibrary获取所有照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 19:52