0

I want to save photo in directory. However I made directories using this code in the view controller.

  NSArray *directoryNames = [NSArray arrayWithObjects:@"hats",@"bottoms",@"right",@"left",nil];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder

    for (int i = 0; i < [directoryNames count] ; i++) {
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[directoryNames objectAtIndex:i]];
        if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; //Create folder

The UIImage is displaying what the user has taken. I would want to save the photo automatically to one of my directories that I made(for example hats) when it is displayed on the UIIMage.Below is my code for displaying to UIImage after photo is taken. Is there away to automatically save to one of the directories??

- (void) processImage:(UIImage *)image { //process captured image, crop, resize and rotate
    haveImage = YES;

    if([UIDevice currentDevice].userInterfaceIdiom==UIUserInterfaceIdiomPad) { //Device is ipad
        // Resize image
        UIGraphicsBeginImageContext(CGSizeMake(768, 1022));
        [image drawInRect: CGRectMake(0, 0, 768, 1022)];
        UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        CGRect cropRect = CGRectMake(0, 130, 768, 768);
        CGImageRef imageRef = CGImageCreateWithImageInRect([smallImage CGImage], cropRect);
        //or use the UIImage wherever you like

        [captureImage setImage:[UIImage imageWithCGImage:imageRef]];

        CGImageRelease(imageRef);

    }else{ //Device is iphone
        // Resize image
        UIGraphicsBeginImageContext(CGSizeMake(320, 426));
        [image drawInRect: CGRectMake(0, 0, 320, 426)];
        UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        CGRect cropRect = CGRectMake(0, 55, 320, 320);
        CGImageRef imageRef = CGImageCreateWithImageInRect([smallImage CGImage], cropRect);

        [captureImage setImage:[UIImage imageWithCGImage:imageRef]];

        CGImageRelease(imageRef);
    }

    //adjust image orientation based on device orientation
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) {
        NSLog(@"landscape left image");

        [UIView beginAnimations:@"rotate" context:nil];
        [UIView setAnimationDuration:0.5];
        captureImage.transform = CGAffineTransformMakeRotation(DegreesToRadians(-90));
        [UIView commitAnimations];

    }
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) {
        NSLog(@"landscape right");

        [UIView beginAnimations:@"rotate" context:nil];
        [UIView setAnimationDuration:0.5];
        captureImage.transform = CGAffineTransformMakeRotation(DegreesToRadians(90));
        [UIView commitAnimations];

    }
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) {
        NSLog(@"upside down");
        [UIView beginAnimations:@"rotate" context:nil];
        [UIView setAnimationDuration:0.5];
        captureImage.transform = CGAffineTransformMakeRotation(DegreesToRadians(180));
        [UIView commitAnimations];

    }
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) {
        NSLog(@"upside upright");
        [UIView beginAnimations:@"rotate" context:nil];
        [UIView setAnimationDuration:0.5];
        captureImage.transform = CGAffineTransformMakeRotation(DegreesToRadians(0));
        [UIView commitAnimations];
    }
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)switchCamera:(id)sender { //switch cameras front and rear cameras
    if (cameraSwitch.selectedSegmentIndex == 0) {
        FrontCamera = YES;
        [self initializeCamera];
    }
    else {
        FrontCamera = NO;
        [self initializeCamera];
    }
}
AppleDevram
  • 67
  • 1
  • 12
  • what do you mean, 'automatically'? – Carl Veazey Aug 15 '13 at 03:09
  • I am making the user take a picture and is displayed in the UIImage. I want the picture that is displayed in UIImage saved into "one" of my directories that I made. sorry for the confusion. – AppleDevram Aug 15 '13 at 03:14
  • /Do you want a directory picked automatically? – Carl Veazey Aug 15 '13 at 03:19
  • Yes as I have in the code I have made directories. I just want to save the photo to directory "right" – AppleDevram Aug 15 '13 at 03:22
  • So by what rule are you supposed to pick a directory? – Carl Veazey Aug 15 '13 at 03:26
  • I don't get your pick directory. I just want to save the pictures into the directory that I made called left. Look at my first code . I don't want to make the user choose which directory they want to save to. – AppleDevram Aug 15 '13 at 03:29
  • I'm sorry but I don't understand. What problem are you having, specifically, with saving the photo? – Carl Veazey Aug 15 '13 at 03:56
  • OK I will simplify the question. I do not know how to save into a directory that "I" have made(LOOK AT THE TOP I HAVE MADE DIRECTORIES named hats bottom right and left. I just simply want to save the photo that is **displaying** in the UIImage AUTOMATICALLY (saying that user does not have to do anything like pushing save button) to the directory called left. The UIImage is showing the picture that the **user** has taken. I cannot simplify my question more.. Which part do you not understand??? – AppleDevram Aug 15 '13 at 04:06
  • So, like this? http://stackoverflow.com/questions/6821517/save-an-image-to-application-documents-folder-from-uiview-on-ios – Carl Veazey Aug 15 '13 at 04:37
  • Yes But I want to save it to a specific directory that I have made. which I can't do. – AppleDevram Aug 15 '13 at 04:43

1 Answers1

0

You probably want to put this at the bottom of processImage:

NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:directoryNames[2]]; // "right" is at index 2, per comments & code
NSString *filePath = [folderPath stringByAppendingPathComponent:@"IMAGE_NAME_HERE.PNG"]; // you maybe want to incorporate a timestamp into the name to avoid duplicates
NSData *imageData = UIImagePNGRepresentation(captureImage.image);
[imageData writeToFile:filePath atomically:YES];
Carl Veazey
  • 18,392
  • 8
  • 66
  • 81