JSA986
JSA986

Reputation: 5936

trying to save image picked to appdocs-not happening

I'm really struggling with this,Day three of the unending quest to save and load an image in my app. I'm picking an image from camera roll and trying to save it to the device via the appsdocsdirectory.

in the Appdelegate.m I have:

// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

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

in the class I want to save and load the image (picked from camera roll into UI imageView)

- (IBAction)save:(id)sender {
    UIImage *myImage = [imageView image];
    NSData *data = UIImagePNGRepresentation(myImage);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *appDocsDirectory = [paths objectAtIndex:0];
}

- (IBAction)load:(id)sender {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *appDocsDirectory = [paths objectAtIndex:0];
    UIImage* thumImage = [UIImage imageWithContentsOfFile: [NSString stringWithFormat:@"%@/%@.png", appDocsDirectory, @"myNewFile"]];
}
@end

I also imported Appdelegate.h, in this class, not sure if that was needed or correct?Now I have finally got rid of all errors and no exceptions being thrown but now my problem is nothing happens when I try to save the image and load it. I'm also getting yellow triangles telling me i have unused veriables in load UIImage* thumImage NSString *appDocsDirectory & NSData *data so I may have made a royal hash of this.

Upvotes: 0

Views: 87

Answers (2)

JSA986
JSA986

Reputation: 5936

I have used NSuser defaults, all sorted

Upvotes: 0

Titouan de Bailleul
Titouan de Bailleul

Reputation: 12949

This is how I did it in one of my project.

I get the picture with the delegate method ot the imagePickerController :

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    pictureInfo = info;
}

Then I save it like this :

-(void)createPin
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pinFolderName = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]];
    NSString *pinFolderPath = [documentsDirectory stringByAppendingPathComponent:pinFolderName];
    [fileManager createDirectoryAtPath:pinFolderPath withIntermediateDirectories:YES attributes:nil error:NULL];
    self.fullsizePath = [pinFolderPath stringByAppendingPathComponent:@"fullsize.png"];
    NSString *mediaType = [pictureInfo objectForKey:UIImagePickerControllerMediaType];   
    if ([mediaType isEqualToString:@"public.image"]){
        UIImage *fullsizeImage = [pictureInfo objectForKey:UIImagePickerControllerEditedImage];
        NSData *fullsizeData = UIImagePNGRepresentation(fullsizeImage);
        [fullsizeData writeToFile:self.fullsizePath atomically:YES];
    }
}

Hope you can start from there. If you need any explanations or help, feel free to ask

Upvotes: 1

Related Questions