0

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.

Community
  • 1
  • 1
JustMe
  • 306
  • 1
  • 4
  • 15
  • 1
    This is too broad. What have you tried? What part are you having an issue with? Break the problem down into discrete steps. – rmaddy Mar 20 '15 at 20:18
  • @rmaddy updated post with more information. The issue is the fact that the image is not showing up right, and I just wanted to redo the entire thing properly. – JustMe Mar 20 '15 at 20:41
  • 1
    The first two lines of `imagePickerController:didFinishPickingMedia:` is fine. The rest is off. Why do you convert the image's PNG data into a string? Just write the data to a file. Using `NSUserDefaults` is a bad idea. Simply write the image to a file. – rmaddy Mar 20 '15 at 20:44
  • @rmaddy That's the issue. I have no idea how to do that. The NSUserDefaults allows me to keep the "[insert image name here].png/jpeg/whatever" in a plist file so the game can easily access it. I'm not sure how to write an image to a file, and even less so how to call it up for the game. – JustMe Mar 20 '15 at 20:46
  • 1
    See http://stackoverflow.com/questions/28496019/how-to-save-image-to-application-folder-from-uiimagepickercontroller – rmaddy Mar 20 '15 at 20:50
  • @rmaddy I put the code in, but when the app loads up, either there is the white box with the red "X" again, or the default ship loads up, as I told it to do if there is nothing to load. – JustMe Mar 20 '15 at 21:08
  • @rmaddy I'm not exactly sure what's up, but I closed Xcode to go practice piano, came back an hour later, and it worked with my modifications. Thanks a lot for the help! – JustMe Mar 20 '15 at 22:16

1 Answers1

0

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.

Community
  • 1
  • 1
JustMe
  • 306
  • 1
  • 4
  • 15