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.