亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用AssetsLibrary框架從iPhone photoLibrary數組中獲取所有圖片?

使用AssetsLibrary框架從iPhone photoLibrary數組中獲取所有圖片?

紅顏莎娜 2019-11-29 10:41:44
我想從photoLibrary獲取所有圖片。我希望可以直接使用的方法或示例。
查看完整描述

3 回答

?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

從ALAssetsLibrary現在開始不推薦使用,并且Photo Framework是新的。我做了自己的功能,Objective C從“相機膠卷”中獲取所有照片并存儲NSArray并顯示在我的Collectionview中


 NSArray *imageArray;

 NSMutableArray *mutableArray;


-(void)getAllPhotosFromCamera

{

    imageArray=[[NSArray alloc] init];

    mutableArray =[[NSMutableArray alloc]init];


    PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];

    requestOptions.resizeMode   = PHImageRequestOptionsResizeModeExact;

    requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;

    requestOptions.synchronous = true;

    PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];


    NSLog(@"%d",(int)result.count);


    PHImageManager *manager = [PHImageManager defaultManager];

    NSMutableArray *images = [NSMutableArray arrayWithCapacity:[result count]];


    // assets contains PHAsset objects.


    __block UIImage *ima;

    for (PHAsset *asset in result) {

        // Do something with the asset


        [manager requestImageForAsset:asset

                           targetSize:PHImageManagerMaximumSize

                          contentMode:PHImageContentModeDefault

                              options:requestOptions

                        resultHandler:^void(UIImage *image, NSDictionary *info) {

                            ima = image;


                            [images addObject:ima];

                        }];



    }


    imageArray = [images copy];  // You can direct use NSMutuable Array images

}


查看完整回答
反對 回復 2019-11-29
?
白衣染霜花

TA貢獻1796條經驗 獲得超10個贊

-(void)getFromGallery:(BOOL )IsImages

{

    if(self.csCollectionsArray != nil)

        [self.csCollectionsArray removeAllObjects];

    __block NSMutableDictionary *date = [[NSMutableDictionary alloc] init];

    ALAssetsLibrary *csAssetsLibrary = [[ALAssetsLibrary alloc] init];

    NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent | ALAssetsGroupFaces | ALAssetsGroupSavedPhotos;

    [csAssetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:^(ALAssetsGroup *group, BOOL *stop)

    {

        if([group numberOfAssets] > 0)

        {

            if(IsImages)

                [group setAssetsFilter:[ALAssetsFilter allPhotos]];

            else

                [group setAssetsFilter:[ALAssetsFilter allVideos]];

            [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)

            {

                if(asset)

                { //1.fetching all assets from device library

                    //2.Add all fetched assests from library

                    [date setObject:asset forKey:[asset valueForProperty:ALAssetPropertyDate]];

                }

            }];

        }

        else

        { NSLog(@"---> load table -------->");

            if(date != nil && date.count > 0)

            { //3.Sort using date by ascending order and moved to dictionary to array

                NSArray *sortedKeys = [[date allKeys] sortedArrayUsingSelector: @selector(compare:)];

                for (NSString *key in sortedKeys)

                    [self.csCollectionsArray addObject: [date objectForKey:key]];

                //4.Load images into collection view after fetching all datas

                [self reloadCollectionView];

                if(self.csCollectionView != nil)

                    [self.csCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:([self.csCollectionsArray count] - 1) inSection:0] atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];

            }

            date = nil;

        }

    }failureBlock:^(NSError *error)

    {

        if((csCollectionsArray == nil || [csCollectionsArray count] == 0))

        {

            ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; if(status != ALAuthorizationStatusAuthorized)

            {

                [self showAlertAndCloseUploaderView:@"You can just go to \"Settings\" app (General -> Reset -> Reset Location & Privacy) then come again and click ok when the alert dialog is showing for enable the permission to access the photo library"];

            }

        }

    }];

}

您可以使用以下方法從ios中的資產庫中獲取所有圖像或視頻。使用此資產庫框架(必須)注:-#import


-(void)getFromGallery:(BOOL )IsImages

{

    if(self.csCollectionsArray != nil)

        [self.csCollectionsArray removeAllObjects];

    __block NSMutableDictionary *date = [[NSMutableDictionary alloc] init];

    ALAssetsLibrary *csAssetsLibrary = [[ALAssetsLibrary alloc] init];

    NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent | ALAssetsGroupFaces | ALAssetsGroupSavedPhotos;

    [csAssetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:^(ALAssetsGroup *group, BOOL *stop)

    {

        if([group numberOfAssets] > 0)

        {

            if(IsImages)

                [group setAssetsFilter:[ALAssetsFilter allPhotos]];

            else

                [group setAssetsFilter:[ALAssetsFilter allVideos]];

            [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)

            {

                if(asset)

                { //1.fetching all assets from device library

                    //2.Add all fetched assests from library

                    [date setObject:asset forKey:[asset valueForProperty:ALAssetPropertyDate]];

                }

            }];

        }

        else

        { NSLog(@"---> load table -------->");

            if(date != nil && date.count > 0)

            { //3.Sort using date by ascending order and moved to dictionary to array

                NSArray *sortedKeys = [[date allKeys] sortedArrayUsingSelector: @selector(compare:)];

                for (NSString *key in sortedKeys)

                    [self.csCollectionsArray addObject: [date objectForKey:key]];

                //4.Load images into collection view after fetching all datas

                [self reloadCollectionView];

                if(self.csCollectionView != nil)

                    [self.csCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:([self.csCollectionsArray count] - 1) inSection:0] atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];

            }

            date = nil;

        }

    }failureBlock:^(NSError *error)

    {

        if((csCollectionsArray == nil || [csCollectionsArray count] == 0))

        {

            ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; if(status != ALAuthorizationStatusAuthorized)

            {

                [self showAlertAndCloseUploaderView:@"You can just go to \"Settings\" app (General -> Reset -> Reset Location & Privacy) then come again and click ok when the alert dialog is showing for enable the permission to access the photo library"];

            }

        }

    }];

}

查看完整回答
反對 回復 2019-11-29
  • 3 回答
  • 0 關注
  • 776 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號