从照片库加载图像并按日期排序时出现内存问题

时间:2013-04-23 06:32:15

标签: iphone ios photolibrary

我从照片库中提取图像,根据资产的日期属性,我将这些图像排列在不同的文件夹中。我想要做的是从照片库中获取图像,根据日期存储它们。例如如果获取的图像是从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;
}

1 个答案:

答案 0 :(得分:0)

您在项目中使用ARC吗?因为您尚未发布任何正在分配和初始化的对象。

resourceDocPath
format
image
bundleRoot

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