美文网首页
相册获取

相册获取

作者: zcc_ios | 来源:发表于2018-08-17 17:00 被阅读17次
    • 之前的代码 自己看看
    #pragma mark 相册操作
    - (void)getOriginalImages{       
    //获取所有自定义相簿    
        PHFetchResult*assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];   
    //便利所有相册簿    
        for (PHAssetCollection *assetCollection in assetCollections) {       
            [self enumerateAssetsInAssetCollection:assetCollection original:YES];       
          NSLog(@"相册名%@",assetCollection.localizedTitle);    
        }   
    //    PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].lastObject;
    //    [self enumerateAssetsInAssetCollection:cameraRoll original:YES];
    }
    //获取所有相册中的缩略图 //没用
    - (void)getThumbnailImages{        
        PHFetchResult*assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];        
        for (PHAssetCollection *assetCollection in assetCollections) {        
            [self enumerateAssetsInAssetCollection:assetCollection original:NO];  
         }
        PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].lastObject;   
        [self enumerateAssetsInAssetCollection:cameraRoll original:NO];
    }
    /* 遍历相册中的原图 @param assetCollection 相册 @param original 是否要原图  */
    - (void)enumerateAssetsInAssetCollection:(PHAssetCollection *)assetCollection original:(BOOL)original{        
        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];    
    //同步获得智慧返回一张    options.synchronous = YES;      
    //获得某个相册中所有的phasset对象   
        PHFetchResult*assets = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];
    //    NSLog(@"图片数量:%lu",(unsigned long)assets.count);
        if(assets.count){
            ZCCPhoneAlbumModel *albumModel = [[ZCCPhoneAlbumModel alloc] init];
    //        NSLog(@"相册名%@",assetCollection.localizedTitle);
    //相册名给对象
            albumModel.photoAlbumName = assetCollection.localizedTitle;
            NSMutableArray *picsArray = [NSMutableArray array];
            for (PHAsset *asset in assets) {
    //        CGSize size = original ? CGSizeMake(asset.pixelWidth, asset.pixelHeight) :         CGSizeZero;
    //宽大于高 就把高变成 asset.pixelHeight * COLLECTIONWIDTH/asset.pixelHeight
                CGFloat width = 0;
                CGFloat height = 0;
    //宽大于高 压缩高
                if(asset.pixelWidth > asset.pixelHeight){
                    width = asset.pixelWidth * COLLECTIONWIDTH/asset.pixelHeight;
                    height = COLLECTIONWIDTH;
                }else{//宽小于高 压缩宽
                    width = COLLECTIONWIDTH;
                    height = asset.pixelHeight * COLLECTIONWIDTH/asset.pixelWidth;
                }
                CGSize size = CGSizeMake(width, height);
    //从asset中获得图片
                [[PHImageManager defaultManager] requestImageForAsset:asset 
                                                 targetSize:size 
                                                 contentMode:PHImageContentModeDefault 
                                                 options:options 
                                                 resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
    //                NSLog(@"%@",result);
                                                    [picsArray addObject:result];
                                                 }];
             }
              albumModel.photos = picsArray;
              //把这个相册对象添加到相册数组中
              [self.photoAlbums addObject:albumModel];
         }
    }
    其实这里可以通过 打开collectionView获取contentoffsize来判断当前滑动位置 然后 加载 总图片的对应阶段图片 边滑动边加载 这样就不会造成刚点开的卡顿了  有空实现一下。
    

    相关文章

      网友评论

          本文标题:相册获取

          本文链接:https://www.haomeiwen.com/subject/pojtiftx.html