4

I'm trying to imitate presenting a photo selection similar to the iPhone Messages app by embedding a scrollview with the photos inside a UIAlertController. To fetch the photos I use the Photos library like so:

- (void)showMediaSelection {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
CGFloat margin = 8.0F;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(margin, margin, alertController.view.bounds.size.width - margin * 4.0F + 2, 100.0F)];
[alertController.view addSubview:scrollView];

PHFetchOptions *fetchOptions = [PHFetchOptions new];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
PHFetchResult *result = [PHAsset fetchAssetsWithOptions:fetchOptions];
__block int x = 2 * margin;
self.scrollableAssets = [NSMutableArray array];

[result enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
    [self.photoManager requestImageForAsset:asset targetSize:CGSizeMake(240, 240)
                                contentMode:PHImageContentModeAspectFit
                                    options:self.imageRequestOptions
                              resultHandler:^(UIImage *image, NSDictionary *info) {

        CGFloat oldHeight = image.size.height;
        CGFloat scaleFactor = oldHeight / 100;
        UIImage *finalImage = [UIImage imageWithCGImage:image.CGImage scale:scaleFactor orientation:UIImageOrientationUp];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, finalImage.size.width, 100)];
        imageView.tag = idx;
        [self.scrollableAssets addObject:asset];
        x = x + finalImage.size.width + margin;
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.image = finalImage;

        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapScrollableAsset:)]];
        imageView.userInteractionEnabled = YES;
        [scrollView addSubview:imageView];

    }];

}];
scrollView.contentSize = CGSizeMake(x, 100.0f);
self.mediaScrollView = scrollView;
__weak ConversationViewController *weakSelf = self;
UIAlertAction *firstAction = [UIAlertAction actionWithTitle:@"Photo Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
    if (weakSelf.selectedImageIndexes.count > 0) {
        NSMutableArray *assets = [NSMutableArray array];
        [weakSelf.selectedImageIndexes enumerateObjectsUsingBlock:^(NSNumber *index, NSUInteger idx, BOOL * stop) {
            [assets addObject:self.scrollableAssets[[index integerValue]]];
        }];
        [weakSelf sendAssets:assets];
        weakSelf.selectedImageIndexes = nil;
    } else {
        // present photo browser
        [weakSelf showPhotoLibrary];
    }

}];
[alertController addAction:firstAction];
self.firstAction = firstAction;

UIAlertAction *secondAction = [UIAlertAction actionWithTitle:@"Take Photo or Video" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
    // present camera
    if (weakSelf.selectedImageIndexes.count > 0) {
        NSMutableArray *assets = [NSMutableArray array];
        [weakSelf.selectedImageIndexes enumerateObjectsUsingBlock:^(NSNumber *index, NSUInteger idx, BOOL * stop) {
            [assets addObject:self.scrollableAssets[[index integerValue]]];
        }];

        [weakSelf showPhotosViewControllerWithAssets:assets];
        weakSelf.selectedImageIndexes = nil;
    } else {
        presentCamera(weakSelf, NO);
    }
}];

[alertController addAction:secondAction];
self.secondAction = secondAction;

[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
    weakSelf.selectedImageIndexes = nil;
    weakSelf.mediaScrollView = nil;
    weakSelf.scrollableAssets = nil;
}]];

[self presentViewController:alertController animated:YES completion:nil];
}

I'm able to display the images: alertcontroller

The problem is everytime I present the UIAlertController, memory usage keeps on increasing by around 20MB and does not go down even if the ViewController it's in is deallocated. This true even if all I do is to press Cancel. Memory usage will just keep on increasing until it reaches around 600MB at which point the app is terminated. I'm testing this on an Iphone 6plus running iOS 9.1.

What am I doing wrong?

grgdgr8
  • 41
  • 4

0 Answers0