2

我正在从我的照片库中获取图像,并根据资产的日期属性,将这些图像排列在不同的文件夹中。我想做的是从照片库中获取图像,根据日期存储它们。例如,如果获取的图像来自 4 月 23 日,则将它们存储在“April”文件夹中。我为此使用 WSAssetPickerController。请帮助我解决内存问题,因为我的应用程序由于设备上的内存问题而崩溃,这在模拟器中运行良好。

- (void)storePhotos:(NSArray *)assets {

    int index = 0;

    for (ALAsset *asset in assets) {

        NSLog(@"Current asset :%@ ",asset);
        NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        [format setDateFormat:@"MMMM dd, yyyy"];

        NSString *photoDate = [format stringFromDate:[asset valueForProperty:ALAssetPropertyDate]];

        resourceDocPath = [resourceDocPath stringByAppendingPathComponent:photoDate];

        // Create directory & Check directory
        BOOL checkDir = [self createDocumentDirectory:resourceDocPath fileName:asset.defaultRepresentation.filename];

        if (!checkDir) {
            continue;
        }

        // Write image
        NSString *writablePhotoPath = [resourceDocPath stringByAppendingPathComponent:asset.defaultRepresentation.filename];
        [self writeImage:writablePhotoPath WithImage:asset.defaultRepresentation.fullScreenImage];

        index++;
    }
}

- (void)writeImage:(NSString*)pstrPhotoPath WithImage:(CGImageRef)pImage {

     if (![[NSFileManager defaultManager] fileExistsAtPath:pstrPhotoPath]){

         UIImage *image = [[UIImage alloc] initWithCGImage:pImage];
         NSData *imageData = UIImagePNGRepresentation(image);
         [imageData writeToFile:pstrPhotoPath atomically:YES];

     }
}

- (BOOL)createDocumentDirectory:(NSString*) pCurrentFolder fileName:(NSString*) pStrFileName  {

    if (![[NSFileManager defaultManager] fileExistsAtPath:pCurrentFolder]){

        if ([self checkPhotoIsExistInDocument:pStrFileName]) {
            return FALSE;
        }

        NSError* error;
        if ([[NSFileManager defaultManager] createDirectoryAtPath:pCurrentFolder withIntermediateDirectories:NO attributes:nil error:&error]) {
            // success
            return TRUE;
        } else {
            NSLog(@"[%@] ERROR: attempting to write create MyFolder directory", [self class]);
            NSAssert( FALSE, @"Failed to create directory maybe out of disk space?");
            return TRUE;
        }
        return TRUE;
    } else {
        return TRUE;
    }
}

- (BOOL)checkPhotoIsExistInDocument:(NSString*) pStrImageName {

    NSString *bundleRoot = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
    NSFileManager *manager = [NSFileManager defaultManager];
    NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:bundleRoot];

    NSString *filename;

    while ((filename = [direnum nextObject] )) {
//        NSLog(@"%@", filename);
        if ([filename hasSuffix:pStrImageName]) {
            return TRUE;
        }
    }

    return FALSE;
}
4

1 回答 1

0

您在项目中使用 ARC 吗?因为您还没有释放任何正在分配和初始化的对象。

resourceDocPath
format
image
bundleRoot

应在使用结束后立即释放。此外,如果您可以提供应用程序崩溃的日志,那将非常有用。

于 2013-04-23T07:03:57.817 回答