2013-03-01 39 views
5

所以我用Core Data來存儲幾張圖像。 (另外,子類與最新mogenerator生成)核心數據永不釋放從外部存儲加載的NSData

(也是我使用ARC)

是的,我知道我可以只保留引用並將其存儲在磁盤上,但我認爲:

「嘿,他們提出了一個選擇,所以我可以做到這一點,而無需自己管理它!」

所以我試了一下,它的工作原理除了所有加載數據的方式都是從未發佈


enter image description here

enter image description here


在ViewController中誰的要負責顯示圖像的初始化我給它平時主要NSManagedObjectContext

而在一個名爲方法viewDidAppear我成立了UIScrollView與圖像:

編輯: 所以它不是一個真正的讀取請求我有一個Entity1其中有一個一對多的圖片和我用它來獲取圖像 我從同一個上下文中獲得這個entity1。我只是想簡化以更好地解釋。

- (void)setupScrollViewWithEntity1:(Entity1 *)entity1 { 
    DDLogVerbose(@"-- %@ : SETUP SCROLL VIEW --", self); 
    // I remove any previous subviews 
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
    self.scrollView.contentSize = self.scrollView.frame.size; 

    /* Here I get the imagesArray with a NSFetchRequest */ 
    //So it's not really a fetch request I have an `Entity1` which have one-to-many with images and I use it to get the images 
    NSSet *imagesSet = entity1.images; 


    // So I have an NSArray holding all the Image object 
    for (Image *image in imagesSet) { 
     CGRect frame = self.scrollView.frame; 
     frame.origin.x = image.numberValue*frame.size.width; 
     UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:frame]; 
     scrollView.contentSize = self.scrollView.frame.size; 
     UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.scrollView.frame]; 
     imageView.image = [UIImage imageWithData:image.image]; 
     imageView.contentMode = UIViewContentModeScaleAspectFit; 
     imageView.userInteractionEnabled = YES; 
     [scrollView addSubview:imageView]; 
     scrollView.delegate = self; 
     scrollView.minimumZoomScale = 1.0; 
     scrollView.maximumZoomScale = 3.0; 
     [self.scrollView addSubview:scrollView]; 
    } 
} 

}

viewWillDisappear我保存NSManagedObjectContext和我希望當控制器被dealloc'ed,所有的數據將過,但它停留在記憶永存。

此行不知何故保留它不知道爲什麼:imageView.image = [UIImage imageWithData:image.image];

我上了3天試圖用一切,Instruments,複查,如果我沒有保持很強的參考地方。

的東西是UIViewController得到dealloc「版,我敢肯定,我看到它在InstrumentsAllocation工具,但由於某種原因在內存中的數據保持永遠,直到應用崩潰


這裏的名單〜20幅圖像在內存沒有得到dealloc'ed:

enter image description here


而這裏的第一個對象的細節:

enter image description here

謝謝你閱讀,直到這裏,我是真的絕望:(

+0

你使用的是弧形嗎? – J2theC 2013-03-01 21:26:30

+0

是的,我忘了提及它,但我標記它讓我編輯 – ItsASecret 2013-03-01 21:27:46

+2

受管理的對象上下文是否超越視圖控制器?如果是這樣,你可能只是得到一個帶有循環引用的對象圖,並且你沒有明確地拒絕你的對象。 – Tommy 2013-03-01 21:35:01

回答

2

你試過呼籲NSManagedObjectContextreset您正在使用的圖像完成後?這會從內存中的對象圖中清除任何加載的對象。然後他們需要在下次需要時從磁盤/分區獲取。

+0

太奇怪了,我確信我已經試過了!但它非常適合謝謝你 – ItsASecret 2013-03-02 09:04:16