Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHImageManager crashing after many image requests

I'm trying to grab all of the users PHAsset with PHAssetMediaTypeImage and then iterate through them, getting the corresponding UIImages one at a time. I have about 2k photos on my iPhone 5 and this code crashes after iterating through 587 of them.

PHFetchResult *fr = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];

PHImageManager *manager = [PHImageManager defaultManager];
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.synchronous = YES;
__block int i = 0;
for (PHAsset *result in fr)
{

    [manager requestImageForAsset:result targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFill options:options resultHandler:^(UIImage *image, NSDictionary *info) {
        NSLog(@"%d", i);
        i++;
    }];
}

The exception reads EXC_BAD_ACCESS (code=1, address=0x0). Any help pointing me in the right direction on this will be enormously appreciated.

like image 636
Stephen Mueller Avatar asked Nov 10 '22 23:11

Stephen Mueller


1 Answers

Closing the loop on this one. The images don't get released until the for loop completes so you need to put an @autoreleasepool to drain the pool after each iteration of the loop, like so

PHFetchResult *fr = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
PHImageManager *manager = [PHImageManager defaultManager];
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.synchronous = YES;
__block int i = 0;
for (PHAsset *result in fr)
{
    @autoreleasepool {
    [manager requestImageForAsset:result targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFill options:options resultHandler:^(UIImage *image, NSDictionary *info) {
            NSLog(@"%d", i);
            i++;
        }];
    }
}
like image 108
Stephen Mueller Avatar answered Nov 14 '22 21:11

Stephen Mueller