我试图通过在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];
}
我能够显示图像: alertcontroller
问题是每当我呈现UIAlertController时,内存使用量会持续增加大约20MB,并且即使取消分配它所在的ViewController也不会下降。这是真的,即使我所做的只是按取消。内存使用量将继续增加,直到达到大约600MB,此时应用程序终止。我正在运行iOS 9.1的Iphone 6plus上进行测试。
我做错了什么?