0

I am trying to save a image to one of my directories that I made like this.

- (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];

        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

        NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"hats"]; 
        NSString *filePath = [filePath stringByAppendingPathComponent:@"IMAGE_NAME_HERE.PNG"]; 
        NSData *imageData = UIImagePNGRepresentation(captureImage.image);
        [imageData writeToFile:filePath atomically:YES];  

However It does not work since I have made a collection view so that I can see what is saved. I would show the code for it if asks. Also I am getting warnings saying at the end of my code saying unused variable folder path and Variable file path is uninitialized when used within in its own initialization. My final goal is that user takes photo in directory hats. And it shows up in a collection view that is getting photo from directory hats. Is there any flaw in my code?

Shouri
  • 39
  • 6

1 Answers1

0

You're doing this:

NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"hats"]; 
NSString *filePath = [filePath stringByAppendingPathComponent:@"IMAGE_NAME_HERE.PNG"]; 

When I think you mean to be doing this:

NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"hats"]; 
NSString *filePath = [folderPath stringByAppendingPathComponent:@"IMAGE_NAME_HERE.PNG"];

Basically, you have an unused variable warning for folderPath because you mistakenly wrote NSString *filePath = [filePath ... which ignores folder path altogether, creates a blank string, and then appends your file name to it. If you logged this string you would see that it outputs only your file name, without the path to the documents directory prepended to it.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • Thanks the warning has disappeared. How can I check if the save has worked if you are not using a simulator? And also do I have to write more codes saying that the photo that I want to save is in the UIImage? I cannot use the simulator since as soon as the image is taken it is displayed in the UIImage. – Shouri Aug 16 '13 at 14:24
  • @Shouri Well, check the top answer here:http://stackoverflow.com/a/8376586/716216 it has a description of how to list all the files in the documents directory and you can use that to determine if the file saved. And no, not exactly. You don't actually save a UIImage but a png representation of the image. When it comes time to read the image, you'll just use `[UIImage imageWithContentsOfFile:`. And if this answer has solved your original problem, please remember to mark as correct :) – Mick MacCallum Aug 16 '13 at 14:28
  • Can you explain the last code `[UIImage imageWithContentsOfFile:` Does this mean that my code is all set to save? or do I need to put the code `[UIImage imageWithContentsOfFile:` ? Is saving a picture that is displayed in the UIImage this simple using about 4 codes? sorry for lots of questions – Shouri Aug 16 '13 at 14:34
  • @Shouri It's hard to say if your code works or not, but no you do not need to add `[UIImage imageWithContentsOfFile:` *yet*. You will only need this code when you want to read the image back out of the documents directory. – Mick MacCallum Aug 16 '13 at 14:36