当前位置:首页 > 移动开发 > 正文

iOS-使用ALAssetsLibrary获取相册图片视频

直接上代码:

 

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                           usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                               
                               // Filter down to only photos
                               [group setAssetsFilter:[ALAssetsFilter allPhotos]];
                               
                               // Grab the first photo return
                               [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:0]
                                                       options:0
                                                    usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                                                        
                                                        if (result) {
                                                            
                                                            id repressentation = [result defaultRepresentation];
                                                            
                                                            // 可以生产一个AVAsset
                                                            NSURL *url = [repressentation url];
                                                            AVAsset *asset = [AVAsset assetWithURL:url];
                                                            
                                                            // 也可以直接获取image
                                                            self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithCGImage:[repressentation fullResolutionImage]]];
                                                            
                                                        }
                                                    }];
                           } failureBlock:^(NSError *error) {
                               
                               NSLog(@"Error: %@", [error localizedDescription]);
                           }];