【问题标题】:Get images without using ImagePickerController不使用 ImagePickerController 获取图像
【发布时间】:2016-04-19 20:56:35
【问题描述】:

我一直试图通过 PHAsset 不使用 UIImagePickerController 来获取画廊照片。当我从 PHAsset 获取图像时,它会占用大量内存..有没有其他方法可以在不使用 PHAsset 库的情况下获取照片

【问题讨论】:

    标签: ios objective-c memory-leaks phasset


    【解决方案1】:

    试试这个。

    - (ALAssetsLibrary *)defaultAssetsLibrary
    {
        static dispatch_once_t pred = 0;
        static ALAssetsLibrary *library = nil;
        dispatch_once(&pred, ^{
            library = [[ALAssetsLibrary alloc] init];
        });
        return library;
    }
    
    - (void)loadThumbnails
    {
        photosArray = [[NSMutableArray alloc] init];
        ALAssetsLibrary *assetLibrary = [self defaultAssetsLibrary];
        [assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
            if (group)
            {
                if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:@"Camera Roll"])
                    [self getContentFrom:group withAssetFilter:[ALAssetsFilter allPhotos]];
            }
        } failureBlock:^(NSError *error) {
            NSLog(@"Error Description %@",[error description]);
        }];
    }
    
    - (void) getContentFrom:(ALAssetsGroup *) group withAssetFilter:(ALAssetsFilter *)filter
    {
        [group setAssetsFilter:filter];
        [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
    
            if (result) {
    
                NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] init];
                [tempDictionary setObject:result forKey:@"thumbnail"];
                [tempDictionary setObject:@"0" forKey:@"selected"];
    
                [photosArray addObject:tempDictionary];
            } else {
                //Finish load Assets
                photosArray = [[[photosArray reverseObjectEnumerator] allObjects] mutableCopy];
                [collectionViewMedia reloadData];
            }
        }];
    }
    

    获取权限

    + (void)requestPermissions:(GalleryPermission)callback
    {
        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
        switch (status)
        {
            case PHAuthorizationStatusAuthorized:
                callback(YES);
                break;
            case PHAuthorizationStatusNotDetermined:
            {
                [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus authorizationStatus)
                 {
                     if (authorizationStatus == PHAuthorizationStatusAuthorized)
                         callback(YES);
                     else
                         callback(NO);
                 }];
                break;
            }
            default:
                callback(NO);
                break;
        }
    }
    

    【讨论】:

    • ALAsset 已弃用..可行吗?
    • 是的,但仅用于获取照片库的权限。在答案中获得权限的方式。 (更新)
    猜你喜欢
    • 2020-12-10
    • 2019-08-11
    • 1970-01-01
    • 2020-01-09
    • 2010-10-31
    • 2011-08-21
    • 1970-01-01
    • 2022-01-10
    • 2013-11-06
    相关资源
    最近更新 更多