Ios 从照片库导入图像时内存泄漏
从相机卷导入照片时出现内存问题。我使用故事板和ARC自动参考计数。当我导入第三张或第四张照片时,应用程序崩溃了,事实上,instruments在空闲物理内存下显示没有内存了。我尝试使用@autoreleasepools,但似乎不起作用:Ios 从照片库导入图像时内存泄漏,ios,xcode,Ios,Xcode,从相机卷导入照片时出现内存问题。我使用故事板和ARC自动参考计数。当我导入第三张或第四张照片时,应用程序崩溃了,事实上,instruments在空闲物理内存下显示没有内存了。我尝试使用@autoreleasepools,但似乎不起作用: - (void)viewDidLoad { @autoreleasepool { [super viewDidLoad]; UIBarButtonItem *cameraButton = [[UIBarButtonItem alloc]
- (void)viewDidLoad {
@autoreleasepool {
[super viewDidLoad];
UIBarButtonItem *cameraButton = [[UIBarButtonItem alloc]
initWithTitle:@"Camera"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(useCamera:)];
UIBarButtonItem *cameraRollButton = [[UIBarButtonItem alloc]
initWithTitle:@"Camera Roll"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(useCameraRoll:)];
NSArray *items = [NSArray arrayWithObjects: cameraButton,
cameraRollButton, nil];
[toolbar setItems:items animated:NO];
mouseMoved = 0;
}}
- (IBAction) useCamera: (id)sender
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
[self presentModalViewController:imagePicker
animated:YES];
newMedia = YES;
}else{
NSLog(@"Camera is not available");
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Important message" message:@"Unfortunately the camera is not available on your device." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert1 show];
}
}
- (IBAction) useCameraRoll: (id)sender
{
@autoreleasepool {
if ([self.popoverController isPopoverVisible]) {
[self.popoverController dismissPopoverAnimated:YES];
} else{
}{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
@autoreleasepool {
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = YES;
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:imagePicker];
}
popoverController.delegate = self;
[self.popoverController
presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
newMedia = NO;
}}
}
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.popoverController dismissPopoverAnimated:true];
@autoreleasepool {
NSString *mediaType = [info
objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info
objectForKey:UIImagePickerControllerOriginalImage];
image1.image = image;
if (newMedia)
UIImageWriteToSavedPhotosAlbum(image,
self,
@selector(image:finishedSavingWithError:contextInfo:),
nil);
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
// Code here to support video if enabled
}}
}
-(void)image:(UIImage *)image
finishedSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo
{
if (error) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Save failed"
message: @"Failed to save image"\
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
}
请帮忙!!提前感谢。image1.image如何处理所有权
使用Heapshot查找内存丢失的位置/方式:
有关如何使用Heapshot查找内存裂缝,请参阅:
基本上,这种方法是运行Instruments allocate工具,获取一个heapshot,运行代码的直觉,然后另一个heapshot重复3到4次。这将指示在迭代过程中已分配而未释放的内存
要了解结果,请查看各个分配
如果需要查看对象的保留、释放和自动释放发生在何处,请使用工具:
在仪器中运行,在分配中设置记录参考计数,您必须停止记录以设置选项。使选择器运行、停止录制、搜索ivar datePickerView、向下钻取,您将能够看到所有保留、发布和自动删除发生的位置
我也有同样的问题。
为创建的每个新项目使用2-3 MB。
负责调用方=CGDataProviderCreateWithCopyOfData
必须设置
imagePicker.allowsEditing = NO;
在使用编辑图像时,这似乎不能正确释放