2012-03-31 62 views
0

我在從相機膠捲導入照片時遇到內存問題。我正在使用故事板和ARC(自動引用計數)。當我導入第三張或第四張照片時,應用程序崩潰,實際上儀器在空閒物理內存下顯示沒有剩餘內存。我嘗試使用@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 
{ 

} 

請幫助!提前致謝。

回答

0

image1.image如何處理所有權。

使用Heapshot尋找到/記憶是如何迷失: 至於如何使用Heapshot查找內存creap,請參閱:bbum blog

基本上有方法是運行儀器分配工具,採取heapshot,運行直覺的代碼和另一個heapshot重複3或4次。這將指示在迭代過程中分配並未釋放的內存。

爲了弄清楚結果是否披露了個別分配。

如果你需要看到保留,發佈和自動釋放出現一個對象使用儀器:在儀器

運行,在設定「記錄的引用計數」關於分配(你必須停止記錄設置的選項)。導致選擇器運行,停止錄製,在那裏搜索ivar(datePickerView),向下鑽取,您將能夠看到所有保留,發佈和自動釋放發生的位置。

0

有同樣的問題。 爲每個創建的新項目使用2-3 MB。 負責呼叫者= CGDataProviderCreateWithCopyOfData

必須設置

imagePicker.allowsEditing = NO; 

看來,使用編輯圖像時,這並不正確地釋放。