不使用ImagePickerController获取图像

时间:2016-04-19 11:56:07

标签: ios objective-c memory-leaks phasset

我一直试图通过PHAsset不使用UIImagePickerController来获取图库照片..当我从PHAsset获取图像时需要大量内存..是否有其他方法可以在不使用PHAsset库的情况下获取照片

1 个答案:

答案 0 :(得分: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;
    }
}