读取元数据中的内存压力问题并删除重复项

时间:2014-10-30 06:18:01

标签: ios objective-c iphone xcode6

-(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);
    }

}

我的代码有问题,当我运行此代码时会产生内存压力问题并导致应用程序崩溃。

功能是:

我从服务器和本地资源加载所有图像并使用文件名相互匹配,并从列表中删除重复图像,因此它只能看到一次。

任何人都有解决方案,所以请帮助。

提前感谢。

1 个答案:

答案 0 :(得分:0)

您正在紧密循环中访问内存密集型内容(ALAssetRepresentations)。在这些情况下,本地自动释放池可以帮助ARC减少内存使用量。

在你通过self.assetAry的循环中,将所有内容包装在autoreleasepool中,如下所示:

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