views:

240

answers:

0

I'm trying to write data to the documents folder on the device. I'm getting the data from a subview that gets released the the file is written. The problem is that the view gets released before the file is written. I'm hoping that i'm doing something wrong in the code but i can't see it.

 (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if([alertView tag] == 1) {  
        if (buttonIndex == 1) {             

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

            NSFileManager *defaultManager = [NSFileManager defaultManager];

            if (documentsDirectory) {
                if(![defaultManager fileExistsAtPath:[documentsDirectory stringByAppendingPathComponent:folder]]) {
                    [defaultManager createDirectoryAtPath:[documentsDirectory stringByAppendingPathComponent:folder] attributes:nil];
                }
            }

            NSString *imagesFilePath = [NSString stringWithFormat:@"%@/%@/",documentsDirectory,folder];
            NSString *editorImageFilePath = [NSString stringWithFormat:@"%@editor.png",imagesFilePath];
            NSString *thumbImageFilePath = [NSString stringWithFormat:@"%@thumb.png",imagesFilePath];

            NSData *editorImageData = UIImagePNGRepresentation(myView.imageView.image);

            UIImage *imageToScale = [self drawImageFromArray:self.mainArrayFront image: myView.imageView.image imageRect:drawImageFront.frame];
            UIImage *imageToSave = [ApeUtility imageWithImage:imageToScale scaledToSize:CGSizeMake(128,96)];
            NSData *thumbImageData = UIImagePNGRepresentation(imageToSave);

            if(editorImageFilePath != nil && thumbImageFilePath != nil) {
                NSLog(@"Saving image to path: %@",editorImageFilePath);
                NSLog(@"Saving image to path: %@",thumbImageFilePath);
                BOOL editorImageSuccess = [editorImageData writeToFile:editorImageFilePath atomically:YES];
                BOOL thumbImageSuccess = [thumbImageData writeToFile:thumbImageFilePath atomically:YES];
                if(editorImageSuccess && thumbImageSuccess) {
                    NSLog(@"Saving Images Success");
                }
            }
            else {
                NSLog(@"Saving image failed.");
            }   

            [self.navigationController popViewControllerAnimated:YES];

            if([delegate respondsToSelector:@selector(didEndEditing)]) {
                [delegate didEndEditing];   
            }

        }
        if (buttonIndex == 2) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }

    if ([alertView tag] == 2 && buttonIndex == 1) {
        if(front) {
            [self.mainArrayFront removeAllObjects];
            [self.redoArrayFront removeAllObjects];             
            drawImageFront.image = [UIImage imageNamed:@"trans.png"];
        }       
        else {
            [self.mainArrayBack removeAllObjects];
            [self.redoArrayBack removeAllObjects];          
            drawImageBack.image = [UIImage imageNamed:@"trans.png"];
        }
    }
}