2

I am fetching images from my photo library and according to assets's date property , I am arranging those images in a different folders . What I want to do is to fetch the image from photo library , store them according to date . e.g. if fetched image is from 23rd April, store them in "April" folder . I am using WSAssetPickerController for this. Please help me in solving memory issue as my app crashes due to the memory issue on device, this works fine in simulator.

- (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;
}
iCoder
  • 1,298
  • 1
  • 9
  • 25

1 Answers1

0

Are you using ARC in your project? Because you have not released any objects which you are allocating and initializing.

resourceDocPath
format
image
bundleRoot

Should be released as soon as their use is over. Also if you can provide the logs of your application crash that would be very useful.

Deepesh Gairola
  • 1,252
  • 12
  • 18