1

我有一种方法可以将一堆 UIImages 保存为 JPG,我的应用程序崩溃了,我相信是由于内存没有被释放。我正在使用 UIImageJPEGRepresentation 保存图像,并将其包装在自动释放池中,但它似乎不起作用。

       for (Images *anImage in images) {

            NSAutoreleasePool* p = [[NSAutoreleasePool alloc] init];

            NSString *fileName = [NSString stringWithFormat:@"%@-%i-%i-%i.jpg", appDelegate.currentUserName, appDelegate.reportId, aDefect.defectId, i];

            [UIImageJPEGRepresentation(anImage.image, 1) writeToFile:localImagePath atomically:NO];

            [p drain];

            i++;

        }

当我运行上面的代码时,分析器显示正在使用的内存越来越多,最终崩溃。如果我删除该行 - [UIImageJPEGRepresentation(anImage.image, 1) writeToFile:localImagePath atomically:NO]; 它工作正常。

仅供参考,循环遍历 NSManagedObjects 数组。

任何帮助,将不胜感激!谢谢

这是根据建议的新代码-

- (void) convertImages {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Report" inManagedObjectContext:context];
NSPredicate *predicate = [NSPredicate
                          predicateWithFormat:@"status != %@", @"Leads"];

[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];
[fetchRequest setIncludesPropertyValues:NO];

NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

for (Report *aReport in fetchedObjects) {

    appDelegate.reportId = [aReport.reportId intValue];

    NSArray *defects = [self getAllItemDefects];

    for (ItemDefect *anItemDefect in defects) {

        NSArray *defectImages = [self getImages:[anItemDefect.itemDefectId intValue] isMainImage:NO];

        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(queue, ^(void) {

            int i = 0;

            for (Images *anImage in defectImages) {

                NSString *fileName = [NSString stringWithFormat:@"%@-%i-%i-%i.jpg", appDelegate.currentUserName, [aReport.reportId intValue], [anItemDefect.itemDefectId intValue], i];

                NSString *localImagePath = [documentsDirectory stringByAppendingPathComponent:fileName];

                [UIImageJPEGRepresentation(anImage.image, 1) writeToFile:localImagePath atomically:NO];

                i++;

            }

            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"FINISH: do eventual operations");
            });

        });

    }

}

}

这给了我这个错误 -

在此处输入图像描述

如果我在调度块中加载defectImages 数组,它什么也不做。谢谢你的帮助。

编辑 - 由于 CoreData 不是线程安全的,我已经声明了新的 FetchRequests 和 ObjectContexts 来解决这个问题。我不再遇到错误的访问错误,但我又回到了内存不足的状态。我已经为您简化了代码,但它在这种状态下会产生内存泄漏 -

        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(queue, ^(void) {

            NSFetchRequest *backgroundFetchRequest = [[NSFetchRequest alloc] init];

            NSManagedObjectContext *backgroundContext = [[[NSManagedObjectContext alloc] init] autorelease];
            [backgroundContext setPersistentStoreCoordinator:persistentStoreCoordinator];
            NSEntityDescription *entity = [NSEntityDescription entityForName:@"Images" inManagedObjectContext:backgroundContext];
            NSPredicate *predicate = [NSPredicate
                                      predicateWithFormat:@"itemDefectId=%i AND reportId=%i AND isMainImage=%i", itemDefectId, appDelegate.reportId, NO];

            [backgroundFetchRequest setEntity:entity];
            [backgroundFetchRequest setIncludesPropertyValues:NO];
            [backgroundFetchRequest setPredicate:predicate];

            NSArray *defectImages = [backgroundContext executeFetchRequest:backgroundFetchRequest error:&error];

            NSMutableArray *images = [[NSMutableArray alloc] init];

            for (Images *anImage in defectImages) {
                [images addObject:anImage.image];
            }

            [images release];

            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"FINISH: do eventual operations");
            });
        });
4

1 回答 1

0

这不是好的解决方案,请使用 GCD:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

for (int i=0; i<[images count]; i++) {

    dispatch_async(queue, ^(void) {

        Images *anImage = (Images *)[images objectAtIndex:i];
        NSString *fileName = [NSString stringWithFormat:@"%@-%i-%i-%i.jpg", appDelegate.currentUserName, appDelegate.reportId, aDefect.defectId, i];

        [UIImageJPEGRepresentation(anImage.image, 1) writeToFile:localImagePath atomically:NO];

        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"FINISH to write image %d", i);
        });

    });
}

此外,在您的代码中,文件名是相同的,它不会改变,也不会被使用。你的代码是部分的?

但是,将调度程序用于异步进程。

于 2013-11-15T01:07:49.437 回答