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.
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++;
}];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With