gpt4 book ai didi

ios - 如何在ARC中释放NSMutableArray保存的数据

转载 作者:行者123 更新时间:2023-11-29 10:27:39 27 4
gpt4 key购买 nike

我有一个使用 ELCImagePickerController 的应用程序.它让我的用户最多选择 6 张图像。我已经保存了这些图像并使用它们来实现整个应用程序中其他 View Controller 中的 View 。但是,我需要能够在用户返回应用程序的主菜单后释放或以某种方式删除保存的数据,以便他们可以选择 6 张新照片。我刚刚开始学习 NSMutableArrays 和 NSArrays。

在用户返回菜单之前,我想清除按钮操作上的数据。我正在使用 ARC,但我一直没有找到如何在使用 ARC 的同时发布我的数据的方法。任何帮助,将不胜感激。

viewcontroller.m

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
[self dismissViewControllerAnimated:YES completion:nil];

for (UIView *v in [_scrollView subviews]) {
[v removeFromSuperview];
}
NSLog(@"info :%@", info);
CGRect workingFrame = _scrollView.frame;
workingFrame.origin.x = 0;

NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];

for (int i =0; i<info.count; i++) {

NSDictionary *dict = [info objectAtIndex:i];
if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypePhoto){
if ([dict objectForKey:UIImagePickerControllerOriginalImage]){
UIImage* image=[dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];

UIImageView *imageview =[[UIImageView alloc]init];
imageview.image=[images objectAtIndex:i];
[_scrollView addSubview:imageview];
NSString *imagePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *stringWithIndex = [NSString stringWithFormat:@"MainImage%d.jpg", i];
NSString *imageName = [imagePath stringByAppendingPathComponent:stringWithIndex];
NSData *imageData = UIImageJPEGRepresentation(imageview.image, 1.0);
BOOL result = [imageData writeToFile:imageName atomically:YES];
NSLog(@"Saved to %@? %@", imageName, (result? @"YES": @"NO"));
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;

[_scrollView addSubview:imageview];

workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
} else {
NSLog(@"UIImagePickerControllerReferenceURL = %@", dict);
}
} else if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypeVideo){
if ([dict objectForKey:UIImagePickerControllerOriginalImage]){
UIImage* image=[dict objectForKey:UIImagePickerControllerOriginalImage];

[images addObject:image];

UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;

[_scrollView addSubview:imageview];

workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
} else {
NSLog(@"UIImagePickerControllerReferenceURL = %@", dict);
}
} else {
NSLog(@"Uknown asset type");
}
self.chosenImages = images;
}
[_scrollView setPagingEnabled:YES];
[_scrollView setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
}

prank6ViewController.m

@implementation Prank6ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

NSString *imagePath1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *imageName1 = [imagePath1 stringByAppendingPathComponent:@"MainImage5.jpg"];


_prankImageView.image = [UIImage imageWithContentsOfFile:imageName1];
}


- (IBAction)mainMenuActionBtn:(id)sender {

/// Somehow delete all the saved images from the Array...

[self performSegueWithIdentifier:@"mainMenuSegue" sender:self];

}
@end

最佳答案

至少,我认为 UIImageJPEGRepresentation 返回自动释放的 NSData 对象,因此每个 NSData 对象仍然在自动释放池中,直到 RunLoop 结束或当前自动释放池范围结束的任何地方。使用 @autoreleasepool 如下。

@autoreleasepool {
NSData *imageData = UIImageJPEGRepresentation(imageview.image, 1.0);
BOOL result = [imageData writeToFile:imageName atomically:YES];
NSLog(@"Saved to %@? %@", imageName, (result? @"YES": @"NO"));
}

关于ios - 如何在ARC中释放NSMutableArray保存的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31349936/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com