2014-10-30 63 views
0
-(void) objectParsed_ListAllMedia:(NSDictionary *)dictionary 
{ 
@try { 

     self.viewLoading.hidden=1; 

     [self.arrGaalleryMediaName removeAllObjects]; 
     [self.arrMediaNames removeAllObjects]; 


     if(self.arrOnlyServerImages == nil){ 
      self.arrOnlyServerImages = [[NSMutableArray alloc] init]; 
     } 

     if([self.arrOnlyServerImages count] >0){ 
      [self.arrOnlyServerImages removeAllObjects]; 
     } 


     if (dictionary==nil) { 

      [self.gridCollectionView reloadData]; 
      return; 
     } 
     // Filter Array for Audio file 

     NSPredicate *predicate = [NSPredicate predicateWithFormat: @"type != 'audio' "]; 
     self.arrOnlyServerImages = [NSMutableArray arrayWithArray:[[dictionary objectForKey:@"objects"] filteredArrayUsingPredicate:predicate]]; 



     // Remove duplicate Start //Read Meta Data and Duplicate from Download, Duplicate from upload START 

     dispatch_queue_t backgroundQueue = dispatch_queue_create("com.memreas.myqueue", 0); 

     dispatch_async(backgroundQueue, ^{ 

      NSMutableArray *arr = [NSMutableArray array]; 
      NSMutableIndexSet * indexSet = [NSMutableIndexSet indexSet]; 

      for (int i=0; self.assetAry.count>i; i++) { 

       ALAsset *result =self.assetAry[i]; 
       ALAssetRepresentation *imageRep = [result defaultRepresentation]; 
       NSDictionary * customMetaDic = [imageRep metadata][(NSString*)kCGImagePropertyIPTCDictionary]; 

       if (customMetaDic) { 

        [self.arrMediaNames addObject:customMetaDic[(NSString*)kCGImagePropertyIPTCObjectName]?customMetaDic[(NSString*)kCGImagePropertyIPTCObjectName]:@""]; 
       }else{ 
        [self.arrMediaNames addObject:@""]; 
       } 

       [self.arrGaalleryMediaName addObject:[self getFileNameWithExtensionFromPath:imageRep.url]]; 

      } 


      for (int i=0; self.arrOnlyServerImages.count>i; i++) { 

       NSMutableDictionary* obj = self.arrOnlyServerImages[i]; 
       NSMutableDictionary * dic2 = [NSMutableDictionary dictionaryWithDictionary:obj]; 
       BOOL isArrMedia =[self.arrMediaNames containsObject:dic2[@"media_name"]]; 
       BOOL isGallery =[self.arrGaalleryMediaName containsObject:dic2[@"media_name"]]; 

       if (isArrMedia||isGallery) { 

        dic2[@"isDownloaded"] = [NSNumber numberWithBool:YES]; 
        [indexSet addIndex: isArrMedia?[self.arrMediaNames indexOfObject:dic2[@"media_name"]] :[self.arrGaalleryMediaName indexOfObject:dic2[@"media_name"]]]; 

       }else{ 

        dic2[@"isDownloaded"] = [NSNumber numberWithBool:NO]; 
       } 

       [arr addObject:dic2]; 

      } 

      dispatch_async(dispatch_get_main_queue(), ^{ 

       @try { 

        self.arrOnlyServerImages = arr; 
        [self.assetAry removeObjectsAtIndexes:indexSet]; 
        [self.gridCollectionView reloadData]; 

       } 
       @catch (NSException *exception) { 
        NSLog(@"%@",exception); 
        [self.gridCollectionView reloadData]; 
       } 

      }); 


     }); 

     // Remove duplicate END //Read Meta Data and Duplicate from Download, Duplicate from upload END 

     [self.gridCollectionView reloadData]; 
     [self.gridView.collectionView reloadData]; 
     [self.location performSelector:@selector(stopActivity) withObject:nil afterDelay:2]; 


    } 
    @catch (NSException *exception) { 
     NSLog(@"%@",exception); 
    } 

} 

我有我的代碼問題,雖然我運行此代碼,它會產生內存壓力問題,並崩潰應用程序。內存壓力問題讀取元數據並刪除重複

功能是:

我加載所有從服務器和本地資產的圖像,並相互匹配與文件名,並從列表中刪除重複的圖像,所以它會看到只有一次。

任何人都有解決方案,請大家幫忙。

在此先感謝。

+0

快速瀏覽一下你的代碼 - 我看到很多'Mutable'變量存在的。嘗試優化你的代碼並改用不可變的變量。 – Kampai 2014-10-30 06:25:49

+0

我需要那個不可變的元素,因爲需求就是這樣的。我需要在索引運行時刪除對象。 – Faizan 2014-10-30 06:28:27

+0

你可以創建臨時的'mutable'變量並執行你的操作。再看看變量'dic2' - 你在數組中增加了'mutable'字典。相反,從'NSMutableDictionary'創建'NSDictionary',並在將它添加到數組後將其移除[NSMutableDictionary]。 – Kampai 2014-10-30 06:34:23

回答

0

您正在訪問內存密集型東西(ALAssetRepresentations)。在這些情況下,本地的autoreleasepool可以幫助ARC保持內存使用不變。

,你通過self.assetAry在循環中,一切都包裹在autoreleasepool像這樣:

@autoreleasepool { 
    AlAsset *asset = ... 
    ... 
    // Rest of your code 
} 
+0

謝謝,我會試試這個 – Faizan 2015-04-14 11:04:19