Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用PHImageManager requestImageForAsset时内存泄漏_Ios_Objective C_Iphone_Memory Leaks - Fatal编程技术网

Ios 使用PHImageManager requestImageForAsset时内存泄漏

Ios 使用PHImageManager requestImageForAsset时内存泄漏,ios,objective-c,iphone,memory-leaks,Ios,Objective C,Iphone,Memory Leaks,我试图通过在UIAlertController中嵌入一个带有照片的滚动视图来模拟显示一个类似于iPhone消息应用程序的照片选择。要获取照片,我使用照片库,如下所示: - (void)showMediaSelection { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n" message:nil preferredStyle:UIAlertControll

我试图通过在UIAlertController中嵌入一个带有照片的滚动视图来模拟显示一个类似于iPhone消息应用程序的照片选择。要获取照片,我使用照片库,如下所示:

- (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];
}
我能够显示图像:

问题是每次我展示UIAlertController时,内存使用量都会持续增加约20MB,即使它所在的ViewController被释放,内存使用量也不会下降。这是真的,即使我所做的只是按下取消键。内存使用量将持续增加,直到达到600MB左右,应用程序才会终止。我正在运行iOS 9.1的iPhone6Plus上测试这一点


我做错了什么?

你能解决这个问题吗?也许这个答案会有帮助