Search code examples
ioscamera-roll

How can I select a picture from the camera roll, then save it to the "Supporting Files" folder of my app, then pick up the name of the saved file?


I'm building an iOS game in Objective-C, and part of it is the ability to unlock new "ships". Now, I have a few, but I want a "custom ship" option, where the user selects an image from their camera roll, and then it displays as a ship in-game.

So, I would like to know how to select a picture (don't need the camera), then save it to the Supporting Files folder in the app so the game can call it up. I also need to find out the name of the file so the game can call the correct image.

Any help would be appreciated.

I have tried

-(IBAction)chooseFromLibrary:(id)sender
{

UIImagePickerController *imagePickerController= [[UIImagePickerController alloc]init];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

// image picker needs a delegate so we can respond to its messages
[imagePickerController setDelegate:self];

// Place image picker on the screen
[self presentViewController:imagePickerController animated:YES completion:nil];

}

//delegate method will be called after picking photo either from camera or library
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

NSData *dataImage = [[NSData alloc] init];
dataImage = UIImagePNGRepresentation(image);
NSString *stringImage = [dataImage base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

stringImage = customImage;
}

Which is slightly modified code from an answer on this StackOverflow post.

customImage is the string that gets put into the NSUserDefaults to save it, as shown here:

-(IBAction)enableCustom:(id)sender
{
NSString *playerSprite = customImage;
[[NSUserDefaults standardUserDefaults] setObject:playerSprite forKey:@"playerImage"];

NSString *playerImage = [[NSUserDefaults standardUserDefaults]
                         stringForKey:@"playerImage"];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wow you unlocked custom" message:@"Fail lol" delegate:self cancelButtonTitle:@"ur mean" otherButtonTitles:@"haha ur an more poor then mi", nil];
[alert show];
}

So, the alert works fine, but when I try to play the game, instead of showing the image, it just shows a white box with a large red "X" in it. I'm not exactly sure what's up.


Solution

  • To pick the image and save it to a custom folder, use this code:

    -(IBAction)chooseFromLibrary:(id)sender
    {
    
    UIImagePickerController *imagePickerController= [[UIImagePickerController alloc]init];
    [imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    
    // image picker needs a delegate so we can respond to its messages
    [imagePickerController setDelegate:self];
    
    // Place image picker on the screen
    [self presentViewController:imagePickerController animated:YES completion:nil];
    
    }
    
    //delegate methode will be called after picking photo either from camera or library
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
    
    [self dismissViewControllerAnimated:YES completion:NULL];
    
    UIImage* image;
    
    if([[info valueForKey:@"UIImagePickerControllerMediaType"] isEqualToString:@"public.image"])
    {
    
        image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
    
        NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"custom"];
    
        // Custom Images is your folder name
    
        NSError *error = nil;
    
        if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];
    
        NSString *fileName = [stringPath stringByAppendingFormat:@"/image.jpg"];
    
        NSData *data = UIImageJPEGRepresentation(image, 1.0);
    
        [data writeToFile:fileName atomically:YES];
    
        customImage = fileName;
    }
    }
    

    customImage is a string variable I had set up as an IBOutlet NSString in the header file.

    Then in my "enable" code, I put the customImage string in NSUserDefaults in order to be able to access it in-game. The code also includes an alert with two buttons.

    -(IBAction)enableCustom:(id)sender
    {
    NSString *playerSprite = customImage;
    [[NSUserDefaults standardUserDefaults] setObject:playerSprite forKey:@"playerImage"];
    
    NSString *playerImage = [[NSUserDefaults standardUserDefaults]
                             stringForKey:@"playerImage"];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wow you unlocked custom" message:@"Fail lol" delegate:self cancelButtonTitle:@"ur mean" otherButtonTitles:@"haha ur an more poor then mi", nil];
    [alert show];
    }
    

    Thanks so much to user rmaddy for their help. They referred me to this post, in case you may need it.