直播app開發搭建,ios 獲取手機中所有圖片

zhibo系統開發發表於2023-01-19

直播app開發搭建,ios 獲取手機中所有圖片

首先我們得匯入相關的庫

#import <AssetsLibrary/AssetsLibrary.h>


程式碼:

photo = [[NSMutableArray alloc]init];
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
    dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(dispatchQueue, ^(void){   
            //遍歷所有相簿
            [assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop){   
                [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){
                    NSString *assetType = [result valueForProperty:ALAssetPropertyType];            
                    NSDictionary *url = [result valueForProperty:ALAssetPropertyURLs];
                    if ([assetType isEqualToString:ALAssetTypePhoto])
                    {
                        [photo addObject:url];
                        NSLog(@"url == %@", url);
                    }
                }];
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (photo != nil)
                    {
                        //我不知道這裡來呼叫會不會有什麼不好
                    }
                    
                });
                
            }
                                      failureBlock:^(NSError *error)
             {
                 NSLog(@"failed");
             }];
    });


這樣我們所有的圖片的url 就放到 photo 這個陣列中了,呼叫的話:

 UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 20, 20)];
      ALAssetsLibrary   *lib = [[ALAssetsLibrary alloc] init] ;
        [lib assetForURL:[[photo objectAtIndex:0] valueForKey:@"public.jpeg"] resultBlock:^(ALAsset *asset)
         {
//這裡可以獲取照片的相關資訊
             ALAssetRepresentation *assetRep = [asset defaultRepresentation];
//獲取縮圖
             CGImageRef imgRef = asset.thumbnail;
             imgView.image = [UIImage imageWithCGImage:imgRef
                                                 scale:assetRep.scale
                                           orientation:(UIImageOrientation)assetRep.orientation];
         }
            failureBlock:^(NSError *error)
         {
             NSLog(@"failed!!");
         }];

 

以上就是 直播app開發搭建,ios 獲取手機中所有圖片,更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2932743/,如需轉載,請註明出處,否則將追究法律責任。

相關文章