从照片库导入图像时出现内存泄漏

时间:2012-03-31 12:58:10

标签: ios xcode

从相机胶卷导入照片时出现内存问题。我正在使用storyboard和ARC(自动引用计数)。当我导入第3张或第4张照片时,应用程序崩溃,事实上,仪器会在自由物理内存下显示没有内存。我尝试使用@autoreleasepools,但它似乎不起作用:

- (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
{

}

请帮忙!!提前谢谢。

2 个答案:

答案 0 :(得分:0)

image1.image如何处理所有权。

使用快照查找内存丢失的位置/方式: 有关如何使用“快照”查找内存褶皱,请参阅:bbum blog

基本上有一种方法是运行仪器分配工具,获取快照,运行代码直观和另一个快照重复3或4次。这将指示在迭代期间分配但未释放的内存。

要弄清楚披露的结果,以查看个别分配。

如果您需要查看对象使用仪器的保留,释放和自动释放的位置:

在仪器中运行,在分配中设置“记录参考计数”(您必须停止记录以设置选项)。导致选择器运行,停止记录,搜索那里的ivar(datePickerView),向下钻取,你将能够看到所有保留,释放和自动释放发生的位置。

答案 1 :(得分:0)

有同样的问题。 为每个创建的新项目使用2-3 MB。 负责的调用者= CGDataProviderCreateWithCopyOfData

必须设置

imagePicker.allowsEditing = NO;

在使用编辑图像时,似乎无法正常释放。