【发布时间】:2018-02-13 15:27:19
【问题描述】:
每当我在图像选择器控制器中选择照片时,都会发生内存泄漏。我已经尝试自己研究为什么会发生这种情况,但我没有发现任何接近我的情况。我学到的各种教程在使用图像选择器控制器时也会出现内存泄漏。我所做的只是选择一张照片,然后图像选择器控制器会关闭。我已经尝试找到解决方案很多天了,但是没有运气,我倾向于在寻求帮助之前尝试找到解决方案,非常感谢任何帮助,这是我的代码。
@property (nonatomic, strong) UIImagePickerController *imagePicker;
@property (nonatomic, strong) UIImage *image;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView reloadData];
if (self.image == nil && [self.videoFilePath length] == 0) {
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = NO;
self.imagePicker.videoMaximumDuration = 10;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
[self presentViewController:self.imagePicker animated:YES completion:nil];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImage *newImage = [self resizeImage:self.image toWidth:200 andHeight:200];
self.image = newImage;
if (self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
}
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
// A video was taken/selected!
NSURL *videoUrl = info[UIImagePickerControllerMediaURL];
self.movieUrl = videoUrl;
self.videoFilePath = videoUrl.absoluteString;
if (self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
// Save the video!
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(self.videoFilePath)) {
UISaveVideoAtPathToSavedPhotosAlbum(self.videoFilePath, nil, nil, nil);
}
}
}
[self dismissViewControllerAnimated:YES completion:nil];
}
当我使用我得到的工具时,负责的实例在 Core Foundation 中。 CF 字符串。我不太熟悉在 Objective-C 中调试内存泄漏。我会很感激你的帮助!谢谢。
【问题讨论】:
-
这次泄漏有多大?
-
@matt 304 字节
-
现在做一些数学运算。在启动和退出您的应用程序之间,用户必须执行多少次此操作,并且每次泄漏 304 字节,然后才能达到相当大的数量,例如 1 MB?
-
@matt 超过 3k 次。而且我知道这并不多,但我被告知内存泄漏是一件坏事。可以忽略它们吗?
-
您还有其他选择吗?泄漏来自Apple的代码。你做错的唯一一件事是保留 UIImagePickerController。停止这样做并继续前进。我会给出这个建议作为答案。
标签: ios objective-c memory-leaks uiimagepickercontroller